this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

update pocketpy from v1.x to v2.0.1 (#2714)

* unchange python.c

* add pkpy2 submodule

* pyfunc not updated

* added debug feature

* [dev]boot returns emptyp

* [dev]basic func finished

* [dev]hello world enabled

* [dev]..

* [dev]all bind

* [dev]add valueError

* [dev]fix some bug

* [dev]fix some bugs

* [dev]all api test pass

* [release]..

* [release]pkpy update to v2

squashed commits of [dev]

* [dev]core unchange

* [dev].

* [dev]castfloat+core saved+exception raise

* [dev]fixed mouse()

* [dev]fixed mouse() alloc

* [dev]delete tmp in mouse()

* [dev]add assert, update pkpy.cmake

* [dev]update pkpy submodule

* some fix

* fix IPO issue

* fix time.c

* fix time.c

* fix time.c

* fix time.c

* [dev]assign none to retval() as default

---------

Co-authored-by: BLUELOVETH <blueloveTH@foxmail.com>

authored by

PrimedErwin
BLUELOVETH
and committed by
GitHub
e98d37ac 5e1b0cae

+1031 -1313
+3 -4
.gitmodules
··· 62 62 path = vendor/janet 63 63 url = https://github.com/janet-lang/janet.git 64 64 shallow = true 65 - [submodule "vendor/pocketpy"] 66 - path = vendor/pocketpy 67 - url = https://github.com/nesbox/pocketpy.git 68 - shallow = true 69 65 [submodule "vendor/quickjs"] 70 66 path = vendor/quickjs 71 67 url = https://github.com/nesbox/quickjs.git ··· 90 86 path = vendor/sdl-gpu 91 87 url = https://github.com/aliceisjustplaying/sdl-gpu.git 92 88 branch = master 89 + [submodule "vendor/pocketpy"] 90 + path = vendor/pocketpy 91 + url = https://github.com/PrimedErwin/pocketpy.git
+5 -8
cmake/pocketpy.cmake
··· 4 4 5 5 option(BUILD_WITH_PYTHON "Python Enabled" ${BUILD_WITH_ALL}) 6 6 message("BUILD_WITH_PYTHON: ${BUILD_WITH_PYTHON}") 7 + if(BUILD_WITH_PYTHON) 8 + option(PK_ENABLE_OS "" OFF) 7 9 8 - if(BUILD_WITH_PYTHON) 10 + if(NOT WIN32) 11 + option(PK_BUILD_WITH_IPO "" OFF) 12 + endif() 9 13 10 14 add_subdirectory(${THIRDPARTY_DIR}/pocketpy) 11 15 ··· 36 40 ) 37 41 38 42 target_link_libraries(python PRIVATE pocketpy) 39 - 40 - if(EMSCRIPTEN) 41 - # exceptions must be enabled for emscripten 42 - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fexceptions") 43 - endif() 44 - 45 - 46 43 endif()
+1023 -1301
src/api/python.c
··· 1 1 #include "core/core.h" 2 2 3 - #include "pocketpy_c.h" 4 3 #include <stdio.h> 5 4 #include <string.h> 5 + #include <assert.h> 6 + #include "pocketpy.h" 7 + 8 + //!!! search NOTICE for v1 remains !!! 6 9 10 + /***************DEBUG SESSION*************/ 11 + //cmake -A x64 -DCMAKE_BUILD_TYPE=MinSizeRel -DBUILD_SDLGPU=On -DBUILD_STATIC=On -DBUILD_WITH_ALL=Off -DBUILD_WITH_PYTHON=On .. 12 + //for debug 13 + //cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=MinSizeRel -DBUILD_SDLGPU=On -DBUILD_WITH_ALL=On .. 14 + //for release 7 15 extern bool parse_note(const char* noteStr, s32* note, s32* octave); 16 + static bool py_throw_error(tic_core* core, const char* msg); 8 17 9 - struct CachedNames{ 10 - pkpy_CName _tic_core; 11 - pkpy_CName len; 12 - pkpy_CName __getitem__; 13 - pkpy_CName TIC; 14 - pkpy_CName BOOT; 15 - pkpy_CName SCN; 16 - pkpy_CName BDR; 17 - pkpy_CName MENU; 18 + struct CachedNames 19 + { 20 + py_Name TIC; 21 + py_Name BOOT; 22 + py_Name SCN; 23 + py_Name BDR; 24 + py_Name MENU; 18 25 } N; 19 26 20 - static void pkpy_setglobal_2(pkpy_vm* vm, const char* name){ 21 - pkpy_setglobal(vm, pkpy_name(name)); 22 - } 23 - 24 - static bool get_core(pkpy_vm* vm, tic_core** core) 27 + static tic_core* get_core() 25 28 { 26 - bool ok = pkpy_getglobal(vm, N._tic_core); 27 - if(!ok) return false; 28 - ok = pkpy_to_voidp(vm, -1, (void**) core); 29 - pkpy_pop_top(vm); 30 - return ok; 29 + void* core_pointer = py_getvmctx(); 30 + if (!core_pointer) return NULL; 31 + tic_core* core = (tic_core*)core_pointer; 32 + assert(core_pointer!=NULL); 33 + return core; 31 34 } 32 35 33 - static bool setup_core(pkpy_vm* vm, tic_core* core) 36 + //set _tic_core to the given core 37 + static bool setup_core(tic_core* core) 34 38 { 35 - if (!pkpy_push_voidp(vm, core)) return false; 36 - return pkpy_setglobal(vm, N._tic_core); 39 + py_newint(py_retval(), (py_i64)core); 40 + py_setvmctx((void*)core); 41 + return true; 37 42 } 38 43 39 44 //index should be a positive index 40 - static int prepare_colorindex(pkpy_vm* vm, int index, u8 * buffer) 45 + //_NOTICE: py_peek(-1) takes stack.top, but pkpy.v1 takes stack.top with index 0 46 + //CHECKED 47 + static int prepare_colorindex(py_Ref index, u8* buffer) 41 48 { 42 - if (pkpy_is_int(vm, index)) 49 + if (py_istype(index, tp_int)) 43 50 { 44 51 int value; 45 - pkpy_to_int(vm, index, &value); 52 + value = py_toint(index); 46 53 47 54 if (value == -1) 48 55 return 0; ··· 54 61 } 55 62 else 56 63 { //should be a list then 57 - pkpy_getglobal(vm, N.len); 58 - pkpy_push_null(vm); 59 - pkpy_dup(vm, index); //get the list 60 - pkpy_vectorcall(vm, 1); 61 - 62 - int list_len; 63 - pkpy_to_int(vm, -1, &list_len); 64 - pkpy_pop_top(vm); 65 - 66 - list_len = (list_len < TIC_PALETTE_SIZE)?(list_len):(TIC_PALETTE_SIZE); 67 - 68 - for(int i = 0; i < list_len; i++) 64 + int list_len = py_list_len(index); 65 + list_len = (list_len < TIC_PALETTE_SIZE) ? (list_len) : (TIC_PALETTE_SIZE); 66 + for (int i = 0; i < list_len; i++) 69 67 { 70 - int list_val; 71 - pkpy_dup(vm, index); //get the list 72 - pkpy_get_unbound_method(vm, N.__getitem__); 73 - pkpy_push_int(vm, i); 74 - pkpy_vectorcall(vm, 1); 75 - pkpy_to_int(vm, -1, &list_val); 76 - buffer[i] = list_val; 77 - pkpy_pop_top(vm); 68 + py_ItemRef pylist_item = py_list_getitem(index, i); 69 + buffer[i] = py_toint(pylist_item); 78 70 } 79 - 80 71 return list_len; 81 72 } 82 73 } 83 74 84 - static int py_trace(pkpy_vm* vm) 85 - { 86 - pkpy_CString message; 87 - int color; 75 + /*****************TIC-80 API BEGIN*****************/ 76 + //API is what u bind to module "__main__" in pkpy 77 + //when python use func like btn(id: int) 78 + //it's equal to py_btn(1, [id]), which passes the arg id 79 + //That's an interpreter 88 80 89 - pkpy_dup(vm, 0); 90 - pkpy_py_str(vm); 91 - pkpy_to_string(vm, -1, &message); 92 - pkpy_pop_top(vm); 93 - 94 - pkpy_to_int(vm, 1, &color); 81 + //Structure of APIs: 82 + //claim args 83 + //check arg type 84 + //claim tic_core + get arg 85 + //use api in C 86 + //if needed, write back result to python 95 87 96 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 97 - if (pkpy_check_error(vm)) 98 - { 99 - return 0; 100 - } 101 - 102 - core->api.trace(tic, message, (u8) color); 103 - return 0; 104 - } 105 - 106 - static int py_cls(pkpy_vm* vm) 88 + static bool py_btn(int argc, py_Ref argv) 107 89 { 108 - 109 - int color; 110 - 111 - pkpy_to_int(vm, 0, &color); 112 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 113 - if (pkpy_check_error(vm)) 114 - return 0; 115 - 116 - core->api.cls(tic, (u8) color); 117 - return 0; 118 - } 119 - 120 - static int py_btn(pkpy_vm* vm) 121 - { 122 - 123 90 int button_id; 91 + tic_core* core = get_core(); 92 + 93 + tic_mem* tic = (tic_mem*)core; 124 94 125 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 126 - pkpy_to_int(vm, 0, &button_id); 127 - if(pkpy_check_error(vm)) 128 - return 0; 95 + PY_CHECK_ARG_TYPE(0, tp_int); //check argv[0] type 96 + button_id = py_toint(py_arg(0)); 129 97 98 + //or get button_id from argv[0]? 99 + //button_id = (int)py_toint(py_peek(-1)); 130 100 bool pressed = core->api.btn(tic, button_id & 0x1f); 131 - pkpy_push_bool(vm, pressed); 132 - return 1; 101 + 102 + py_newbool(py_retval(), pressed); 103 + return true; 133 104 } 134 105 135 - static int py_btnp(pkpy_vm* vm) 106 + static bool py_btnp(int argc, py_Ref argv) 136 107 { 137 108 138 109 int button_id; 139 110 int hold; 140 111 int period; 141 112 142 - pkpy_to_int(vm, 0, &button_id); 143 - pkpy_to_int(vm, 1, &hold); 144 - pkpy_to_int(vm, 2, &period); 145 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 146 - if(pkpy_check_error(vm)) 147 - return 0; 113 + PY_CHECK_ARG_TYPE(0, tp_int); 114 + //button_id = py_toint(py_peek(-1)); 115 + //hold = py_toint(py_peek(-2)); 116 + //period = py_toint(py_peek(-3)); 117 + button_id = py_toint(py_arg(0)); 118 + hold = py_toint(py_arg(1)); 119 + period = py_toint(py_arg(2)); 120 + tic_core* core = get_core(); 121 + 122 + tic_mem* tic = (tic_mem*)core; 148 123 149 124 bool pressed = core->api.btnp(tic, button_id, hold, period); 150 - pkpy_push_bool(vm, pressed); 151 - return 1; 125 + py_newbool(py_retval(), pressed); 126 + return true; 152 127 } 153 128 154 - static int py_circ(pkpy_vm* vm) 129 + static bool py_cls(int argc, py_Ref argv) 155 130 { 156 - 157 - int x; 158 - int y; 159 - int radius; 160 131 int color; 132 + PY_CHECK_ARG_TYPE(0, tp_int); 133 + tic_core* core = get_core(); 134 + 135 + tic_mem* tic = (tic_mem*)core; 161 136 162 - pkpy_to_int(vm, 0, &x); 163 - pkpy_to_int(vm, 1, &y); 164 - pkpy_to_int(vm, 2, &radius); 165 - pkpy_to_int(vm, 3, &color); 166 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 167 - if(pkpy_check_error(vm)) 168 - return 0; 169 - 170 - core->api.circ(tic, x, y, radius, color); 171 - return 0; 137 + color = py_toint(py_arg(0)); 138 + core->api.cls(tic, color); 139 + return true; 172 140 } 173 141 174 - static int py_circb(pkpy_vm* vm) 142 + static bool py_spr(int argc, py_Ref argv) 175 143 { 176 - 144 + int spr_id; 177 145 int x; 178 146 int y; 179 - int radius; 180 - int color; 181 - 182 - pkpy_to_int(vm, 0, &x); 183 - pkpy_to_int(vm, 1, &y); 184 - pkpy_to_int(vm, 2, &radius); 185 - pkpy_to_int(vm, 3, &color); 186 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 187 - if(pkpy_check_error(vm)) 188 - return 0; 147 + int color_count; 148 + int scale; 149 + int flip; 150 + int rotate; 151 + int w; 152 + int h; 153 + char colors[16]; 154 + for (int i = 0; i < 9; i++) 155 + PY_CHECK_ARG_TYPE(i, tp_int); 156 + tic_core* core = get_core(); 157 + 158 + tic_mem* tic = (tic_mem*)core; 159 + spr_id = py_toint(py_arg(0)); 160 + x = py_toint(py_arg(1)); 161 + y = py_toint(py_arg(2)); 162 + scale = py_toint(py_arg(4)); 163 + flip = py_toint(py_arg(5)); 164 + rotate = py_toint(py_arg(6)); 165 + w = py_toint(py_arg(7)); 166 + h = py_toint(py_arg(8)); 167 + if (py_istype(py_arg(3), tp_int) || py_istype(py_arg(3), tp_list)) 168 + { //it's one of the types 169 + color_count = prepare_colorindex(py_arg(3), colors); 170 + } 171 + else 172 + return TypeError("The given argument is not int or list"); 189 173 190 - core->api.circb(tic, x, y, radius, color); 191 - return 0; 174 + core->api.spr(tic, spr_id, x, y, w, h, colors, color_count, scale, flip, rotate); 175 + return true; 192 176 } 193 177 194 - static int py_elli(pkpy_vm* vm) 178 + static bool py_print(int argc, py_Ref argv) 195 179 { 180 + const char* str; 181 + int x, y, color, scale; 182 + bool fixed, alt; 196 183 197 - int x; 198 - int y; 199 - int a; 200 - int b; 201 - int color; 184 + PY_CHECK_ARG_TYPE(0, tp_str); 185 + PY_CHECK_ARG_TYPE(1, tp_int); 186 + PY_CHECK_ARG_TYPE(2, tp_int); 187 + PY_CHECK_ARG_TYPE(3, tp_int); 188 + PY_CHECK_ARG_TYPE(4, tp_bool); 189 + PY_CHECK_ARG_TYPE(5, tp_int); 190 + PY_CHECK_ARG_TYPE(6, tp_bool); 202 191 203 - pkpy_to_int(vm, 0, &x); 204 - pkpy_to_int(vm, 1, &y); 205 - pkpy_to_int(vm, 2, &a); 206 - pkpy_to_int(vm, 3, &b); 207 - pkpy_to_int(vm, 4, &color); 208 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 209 - if(pkpy_check_error(vm)) 210 - return 0; 192 + str = py_tostr(py_arg(0)); 193 + x = py_toint(py_arg(1)); 194 + y = py_toint(py_arg(2)); 195 + color = py_toint(py_arg(3)); 196 + fixed = py_tobool(py_arg(4)); 197 + scale = py_toint(py_arg(5)); 198 + alt = py_tobool(py_arg(6)); 199 + tic_core* core = get_core(); 200 + 201 + tic_mem* tic = (tic_mem*)core; 211 202 212 - core->api.elli(tic, x, y, a, b, color); 213 - return 0; 203 + s32 ps = core->api.print(tic, str, x, y, color, fixed, scale, alt); 204 + py_newint(py_retval(), ps); 205 + return true; 214 206 } 215 207 216 - static int py_ellib(pkpy_vm* vm) 208 + static bool py_circ(int argc, py_Ref argv) 217 209 { 210 + int x, y, radius, color; 211 + for (int i = 0; i < 4; i++) 212 + PY_CHECK_ARG_TYPE(i, tp_int); 213 + tic_core* core = get_core(); 214 + 215 + tic_mem* tic = (tic_mem*)core; 216 + x = py_toint(py_arg(0)); 217 + y = py_toint(py_arg(1)); 218 + radius = py_toint(py_arg(2)); 219 + color = py_toint(py_arg(3)); 218 220 219 - int x; 220 - int y; 221 - int a; 222 - int b; 223 - int color; 221 + core->api.circ(tic, x, y, radius, color); 222 + py_assign(py_retval(), py_None()); 223 + return true; 224 + } 224 225 225 - pkpy_to_int(vm, 0, &x); 226 - pkpy_to_int(vm, 1, &y); 227 - pkpy_to_int(vm, 2, &a); 228 - pkpy_to_int(vm, 3, &b); 229 - pkpy_to_int(vm, 4, &color); 230 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 231 - if(pkpy_check_error(vm)) 232 - return 0; 226 + static bool py_circb(int argc, py_Ref argv) 227 + { 228 + int x, y, radius, color; 229 + for (int i = 0; i < 4; i++) 230 + PY_CHECK_ARG_TYPE(i, tp_int); 231 + tic_core* core = get_core(); 232 + 233 + tic_mem* tic = (tic_mem*)core; 234 + x = py_toint(py_arg(0)); 235 + y = py_toint(py_arg(1)); 236 + radius = py_toint(py_arg(2)); 237 + color = py_toint(py_arg(3)); 233 238 234 - core->api.ellib(tic, x, y, a, b, color); 235 - return 0; 239 + core->api.circb(tic, x, y, radius, color); 240 + py_assign(py_retval(), py_None()); 241 + return true; 236 242 } 237 243 238 - static int py_paint(pkpy_vm* vm) 244 + static bool py_clip(int argc, py_Ref argv) 239 245 { 240 - int x; 241 - int y; 242 - int color; 243 - int bordercolor; 244 - 245 - pkpy_to_int(vm, 0, &x); 246 - pkpy_to_int(vm, 1, &y); 247 - pkpy_to_int(vm, 2, &color); 248 - pkpy_to_int(vm, 3, &bordercolor); 249 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 250 - if(pkpy_check_error(vm)) 251 - return 0; 246 + int x, y, width, height; 247 + for (int i = 0; i < 4; i++) 248 + PY_CHECK_ARG_TYPE(i, tp_int); 249 + tic_core* core = get_core(); 250 + 251 + tic_mem* tic = (tic_mem*)core; 252 + x = py_toint(py_arg(0)); 253 + y = py_toint(py_arg(1)); 254 + width = py_toint(py_arg(2)); 255 + height = py_toint(py_arg(3)); 252 256 253 - core->api.paint(tic, x, y, color, bordercolor); 254 - return 0; 257 + core->api.clip(tic, x, y, width, height); 258 + py_assign(py_retval(), py_None()); 259 + return true; 255 260 } 256 261 257 - static int py_clip(pkpy_vm* vm) 262 + static bool py_elli(int argc, py_Ref argv) 258 263 { 259 - 260 - int x; 261 - int y; 262 - int w; 263 - int h; 264 - 265 - pkpy_to_int(vm, 0, &x); 266 - pkpy_to_int(vm, 1, &y); 267 - pkpy_to_int(vm, 2, &w); 268 - pkpy_to_int(vm, 3, &h); 269 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 270 - if(pkpy_check_error(vm)) 271 - return 0; 264 + int x, y, a, b, color; 265 + for (int i = 0; i < 5; i++) 266 + PY_CHECK_ARG_TYPE(i, tp_int); 267 + tic_core* core = get_core(); 268 + 269 + tic_mem* tic = (tic_mem*)core; 270 + x = py_toint(py_arg(0)); 271 + y = py_toint(py_arg(1)); 272 + a = py_toint(py_arg(2)); 273 + b = py_toint(py_arg(3)); 274 + color = py_toint(py_arg(4)); 272 275 273 - core->api.clip(tic, x, y, w, h); 274 - return 0; 276 + core->api.elli(tic, x, y, a, b, color); 277 + py_assign(py_retval(), py_None()); 278 + return true; 275 279 } 276 280 277 - static int py_exit(pkpy_vm* vm) 281 + static bool py_ellib(int argc, py_Ref argv) 278 282 { 283 + int x, y, a, b, color; 284 + for (int i = 0; i < 5; i++) 285 + PY_CHECK_ARG_TYPE(i, tp_int); 286 + tic_core* core = get_core(); 287 + 288 + tic_mem* tic = (tic_mem*)core; 289 + x = py_toint(py_arg(0)); 290 + y = py_toint(py_arg(1)); 291 + a = py_toint(py_arg(2)); 292 + b = py_toint(py_arg(3)); 293 + color = py_toint(py_arg(4)); 279 294 295 + core->api.ellib(tic, x, y, a, b, color); 296 + py_assign(py_retval(), py_None()); 297 + return true; 298 + } 280 299 281 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 282 - if(pkpy_check_error(vm)) 283 - return 0; 300 + static bool py_exit(int argc, py_Ref argv) 301 + { 302 + tic_core* core = get_core(); 303 + 304 + tic_mem* tic = (tic_mem*)core; 284 305 285 306 core->api.exit(tic); 286 - return 0; 307 + py_assign(py_retval(), py_None()); 308 + return true; 287 309 } 288 310 289 - static int py_fget(pkpy_vm* vm) 311 + static bool py_fget(int argc, py_Ref argv) 290 312 { 291 - 292 - int sprite_id; 293 - int flag; 313 + int spid, flag; 314 + PY_CHECK_ARG_TYPE(0, tp_int); 315 + PY_CHECK_ARG_TYPE(1, tp_int); 316 + tic_core* core = get_core(); 317 + 318 + tic_mem* tic = (tic_mem*)core; 319 + spid = py_toint(py_arg(0)); 320 + flag = py_toint(py_arg(1)); 294 321 295 - pkpy_to_int(vm, 0, &sprite_id); 296 - pkpy_to_int(vm, 1, &flag); 297 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 298 - if(pkpy_check_error(vm)) 299 - return 0; 322 + bool res = core->api.fget(tic, spid, flag); 323 + py_newbool(py_retval(), res); 300 324 301 - bool set = core->api.fget(tic, sprite_id, (u8)flag); 302 - pkpy_push_bool(vm, set); 303 - return 1; 325 + return true; 304 326 } 305 327 306 - static int py_fset(pkpy_vm* vm) 328 + static bool py_fset(int argc, py_Ref argv) 307 329 { 330 + int spid, flag; 331 + bool b; 332 + PY_CHECK_ARG_TYPE(0, tp_int); 333 + PY_CHECK_ARG_TYPE(1, tp_int); 334 + PY_CHECK_ARG_TYPE(2, tp_bool); 335 + tic_core* core = get_core(); 336 + 337 + tic_mem* tic = (tic_mem*)core; 338 + spid = py_toint(py_arg(0)); 339 + flag = py_toint(py_arg(1)); 340 + b = py_tobool(py_arg(2)); 308 341 309 - int sprite_id; 310 - int flag; 311 - bool set_to; 312 - 313 - pkpy_to_int(vm, 0, &sprite_id); 314 - pkpy_to_int(vm, 1, &flag); 315 - pkpy_to_bool(vm, 2, &set_to); 316 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 317 - if(pkpy_check_error(vm)) 318 - return 0; 319 - 320 - core->api.fset(tic, sprite_id, (u8)flag, set_to); 321 - return 0; 342 + core->api.fset(tic, spid, flag, b); 343 + py_assign(py_retval(), py_None()); 344 + return true; 322 345 } 323 346 324 - static int py_font(pkpy_vm* vm) 347 + static bool py_font(int argc, py_Ref argv) 325 348 { 349 + const char* str; 350 + int x, y, width, height, scale; 351 + u8 chromakey; 352 + bool fixed, alt; 353 + PY_CHECK_ARG_TYPE(0, tp_str); 354 + PY_CHECK_ARG_TYPE(1, tp_int); 355 + PY_CHECK_ARG_TYPE(2, tp_int); 356 + PY_CHECK_ARG_TYPE(3, tp_int); 357 + PY_CHECK_ARG_TYPE(4, tp_int); 358 + PY_CHECK_ARG_TYPE(5, tp_int); 359 + PY_CHECK_ARG_TYPE(6, tp_bool); 360 + PY_CHECK_ARG_TYPE(7, tp_int); 361 + PY_CHECK_ARG_TYPE(8, tp_bool); 326 362 327 - pkpy_CString text; 328 - int x; 329 - int y; 330 - int width; 331 - int height; 332 - int chromakey_raw; 333 - bool fixed; 334 - int scale; 335 - bool alt; 336 - 337 - pkpy_to_string(vm, 0, &text); 338 - pkpy_to_int(vm, 1, &x); 339 - pkpy_to_int(vm, 2, &y); 340 - pkpy_to_int(vm, 3, &chromakey_raw); 341 - pkpy_to_int(vm, 4, &width); 342 - pkpy_to_int(vm, 5, &height); 343 - pkpy_to_bool(vm, 6, &fixed); 344 - pkpy_to_int(vm, 7, &scale); 345 - pkpy_to_bool(vm, 8, &alt); 346 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 347 - if(pkpy_check_error(vm)) { 348 - return 0; 349 - } 363 + tic_core* core = get_core(); 364 + 365 + tic_mem* tic = (tic_mem*)core; 366 + str = py_tostr(py_arg(0)); 367 + x = py_toint(py_arg(1)); 368 + y = py_toint(py_arg(2)); 369 + chromakey = (u8)py_toint(py_arg(3)); 370 + width = py_toint(py_arg(4)); 371 + height = py_toint(py_arg(5)); 372 + fixed = py_tobool(py_arg(6)); 373 + scale = py_toint(py_arg(7)); 374 + alt = py_tobool(py_arg(8)); 350 375 351 376 if (scale == 0) 352 377 { 353 - pkpy_push_int(vm, 0); 354 - return 1; 378 + py_newint(py_retval(), 0); 379 + return true; 355 380 } 356 381 357 - u8 chromakey = (u8) chromakey_raw; 382 + s32 res = core->api.font(tic, str, x, y, &chromakey, 383 + 1, width, height, fixed, scale, alt); 384 + py_newint(py_retval(), res); 358 385 359 - s32 size = core->api.font(tic, text, x, y, &chromakey, 1, width, height, fixed, scale, alt); 360 - pkpy_push_int(vm, size); 361 - 362 - return 1; 386 + return true; 363 387 } 364 388 365 - static int py_key(pkpy_vm* vm) 389 + static bool py_key(int argc, py_Ref argv) 366 390 { 367 - 368 - int key_id; 369 - 370 - pkpy_to_int(vm, 0, &key_id); 371 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 372 - if(pkpy_check_error(vm)) 373 - return 0; 391 + int code; 392 + PY_CHECK_ARG_TYPE(0, tp_int); 393 + code = py_toint(py_arg(0)); 394 + tic_core* core = get_core(); 395 + 396 + tic_mem* tic = (tic_mem*)core; 374 397 375 - if (key_id >= tic_keys_count) { 376 - pkpy_error(vm, "RuntimeError", pkpy_string("unknown keyboard code\n")); 377 - return 0; 398 + if (code >= tic_keys_count) 399 + { 400 + return ValueError("unknown keyboard input"); 378 401 } 379 402 380 - bool pressed = core->api.key(tic, key_id); 381 - pkpy_push_bool(vm, pressed); 382 - return 1; 403 + bool pressed = core->api.key(tic, code); 404 + py_newbool(py_retval(), pressed); 405 + 406 + return true; 383 407 } 384 408 385 - static int py_keyp(pkpy_vm* vm) 409 + static bool py_keyp(int argc, py_Ref argv) 386 410 { 387 - 388 - int key_id; 389 - int hold; 390 - int period; 391 - 392 - pkpy_to_int(vm, 0, &key_id); 393 - pkpy_to_int(vm, 1, &hold); 394 - pkpy_to_int(vm, 2, &period); 395 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 396 - if(pkpy_check_error(vm)) 397 - return 0; 411 + int code, hold, period; 412 + PY_CHECK_ARG_TYPE(0, tp_int); 413 + PY_CHECK_ARG_TYPE(1, tp_int); 414 + PY_CHECK_ARG_TYPE(2, tp_int); 415 + code = py_toint(py_arg(0)); 416 + hold = py_toint(py_arg(1)); 417 + period = py_toint(py_arg(2)); 418 + tic_core* core = get_core(); 419 + 420 + tic_mem* tic = (tic_mem*)core; 398 421 399 - if (key_id >= tic_keys_count) { 400 - pkpy_error(vm, "RuntimeError", pkpy_string("unknown keyboard code\n")); 401 - return 0; 422 + if (code >= tic_keys_count) 423 + { 424 + return ValueError("unknown keyboard input"); 402 425 } 403 426 404 - bool pressed = core->api.keyp(tic, key_id, hold, period); 405 - pkpy_push_bool(vm, pressed); 406 - return 1; 427 + bool pressed = core->api.keyp(tic, code, hold, period); 428 + py_newbool(py_retval(), pressed); 429 + 430 + return true; 407 431 } 408 432 409 - static int py_line(pkpy_vm* vm) 433 + static bool py_line(int argc, py_Ref argv) 410 434 { 411 - 412 - double x0; 413 - double y0; 414 - double x1; 415 - double y1; 416 - int color; 417 - 418 - pkpy_to_float(vm, 0, &x0); 419 - pkpy_to_float(vm, 1, &y0); 420 - pkpy_to_float(vm, 2, &x1); 421 - pkpy_to_float(vm, 3, &y1); 422 - pkpy_to_int(vm, 4, &color); 423 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 424 - if(pkpy_check_error(vm)) 425 - return 0; 435 + int x0, y0, x1, y1, color; 436 + PY_CHECK_ARG_TYPE(0, tp_int); 437 + PY_CHECK_ARG_TYPE(1, tp_int); 438 + PY_CHECK_ARG_TYPE(2, tp_int); 439 + PY_CHECK_ARG_TYPE(3, tp_int); 440 + PY_CHECK_ARG_TYPE(4, tp_int); 441 + x0 = py_toint(py_arg(0)); 442 + y0 = py_toint(py_arg(1)); 443 + x1 = py_toint(py_arg(2)); 444 + y1 = py_toint(py_arg(3)); 445 + color = py_toint(py_arg(4)); 446 + tic_core* core = get_core(); 447 + 448 + tic_mem* tic = (tic_mem*)core; 426 449 427 450 core->api.line(tic, x0, y0, x1, y1, color); 428 - return 0; 451 + py_assign(py_retval(), py_None()); 452 + return true; 429 453 } 430 454 431 - static void remap_callback(void* data, s32 x, s32 y, RemapResult* result) { 432 - pkpy_vm* vm = data; 433 - 434 - pkpy_dup(vm, -1); //get copy of remap callable 435 - pkpy_push_null(vm); 436 - pkpy_push_int(vm, x); 437 - pkpy_push_int(vm, y); 438 - pkpy_vectorcall(vm, 2); 455 + //NOTICE: untested 456 + static void remap_callback(void* data, s32 x, s32 y, RemapResult* res) 457 + { 458 + py_push(py_peek(-1)); 459 + py_pushnil(); 460 + py_Ref x0 = py_retval(); 461 + py_Ref y0 = py_retval(); 462 + py_newint(x0, x); 463 + py_newint(y0, y); 464 + py_push(x0); 465 + py_push(y0); 466 + py_vectorcall(2, 0); 439 467 440 - pkpy_unpack_sequence(vm, 3); 468 + py_Ref list = py_retval(); 469 + if (!py_checktype(list, tp_list)) return; 441 470 int index, flip, rotate; 442 - pkpy_to_int(vm, -3, &index); 443 - pkpy_to_int(vm, -2, &flip); 444 - pkpy_to_int(vm, -1, &rotate); 445 - pkpy_pop(vm, 3); //reset stack for next remap_callback call 471 + py_ItemRef item = py_list_getitem(list, 0); 472 + index = py_toint(item); 473 + item = py_list_getitem(list, 1); 474 + flip = py_toint(item); 475 + item = py_list_getitem(list, 2); 476 + rotate = py_toint(item); 446 477 447 - result->index = (u8) index; 448 - result->flip = flip; 449 - result->rotate = rotate; 478 + res->index = (u8)index; 479 + res->flip = flip; 480 + res->rotate = rotate; 450 481 } 451 482 452 - static int py_map(pkpy_vm* vm) 483 + static bool py_map(int argc, py_Ref argv) 453 484 { 485 + int x, y, w, h, sx, sy, colorkey, scale; 486 + bool use_remap; 487 + char colors[16]; 454 488 455 - int x; 456 - int y; 457 - int w; 458 - int h; 459 - int sx; 460 - int sy; 461 - int color_count; 462 - int scale; 463 - bool used_remap; 464 - 465 - static u8 colors[TIC_PALETTE_SIZE]; 466 - 467 - pkpy_to_int(vm, 0, &x); 468 - pkpy_to_int(vm, 1, &y); 469 - pkpy_to_int(vm, 2, &w); 470 - pkpy_to_int(vm, 3, &h); 471 - pkpy_to_int(vm, 4, &sx); 472 - pkpy_to_int(vm, 5, &sy); 473 - color_count = prepare_colorindex(vm, 6, colors); 474 - pkpy_to_int(vm, 7, &scale); 475 - used_remap = !pkpy_is_none(vm, 8); 476 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 477 - if(pkpy_check_error(vm)) 478 - return 0; 489 + PY_CHECK_ARG_TYPE(0, tp_int); 490 + PY_CHECK_ARG_TYPE(1, tp_int); 491 + PY_CHECK_ARG_TYPE(2, tp_int); 492 + PY_CHECK_ARG_TYPE(3, tp_int); 493 + PY_CHECK_ARG_TYPE(4, tp_int); 494 + PY_CHECK_ARG_TYPE(5, tp_int); 495 + PY_CHECK_ARG_TYPE(7, tp_int); 496 + x = py_toint(py_arg(0)); 497 + y = py_toint(py_arg(1)); 498 + w = py_toint(py_arg(2)); 499 + h = py_toint(py_arg(3)); 500 + sx = py_toint(py_arg(4)); 501 + sy = py_toint(py_arg(5)); 502 + if (py_istype(py_arg(6), tp_int) || py_istype(py_arg(6), tp_list)) 503 + { //it's one of the types 504 + colorkey = prepare_colorindex(py_arg(6), colors); 505 + } 506 + else 507 + return TypeError("The given argument is not int or list"); 508 + scale = py_toint(py_arg(7)); 509 + use_remap = !py_isnone(py_arg(8)); 510 + tic_core* core = get_core(); 511 + 512 + tic_mem* tic = (tic_mem*)core; 479 513 480 - //last element on the stack should be the function, so no need to adjust anything 481 - if (used_remap) 482 - core->api.map(tic, x, y, w, h, sx, sy, colors, color_count, scale, remap_callback, vm); 514 + if (use_remap) 515 + core->api.map(tic, x, y, w, h, sx, sy, colors, colorkey, scale, remap_callback, core->currentVM); 483 516 else 484 - core->api.map(tic, x, y, w, h, sx, sy, colors, color_count, scale, NULL, NULL); 517 + core->api.map(tic, x, y, w, h, sx, sy, colors, colorkey, scale, NULL, NULL); 485 518 486 - return 0; 519 + py_assign(py_retval(), py_None()); 520 + return true; 487 521 } 488 522 489 - static int py_memcpy(pkpy_vm* vm) { 490 - 491 - 492 - int dest; 493 - int src; 494 - int size; 495 - 496 - pkpy_to_int(vm, 0, &dest); 497 - pkpy_to_int(vm, 1, &src); 498 - pkpy_to_int(vm, 2, &size); 499 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 500 - if(pkpy_check_error(vm)) 501 - return 0; 523 + static bool py_memcpy(int argc, py_Ref argv) 524 + { 525 + int dest, src, size; 526 + PY_CHECK_ARG_TYPE(0, tp_int); 527 + PY_CHECK_ARG_TYPE(1, tp_int); 528 + PY_CHECK_ARG_TYPE(2, tp_int); 529 + dest = py_toint(py_arg(0)); 530 + src = py_toint(py_arg(1)); 531 + size = py_toint(py_arg(2)); 532 + tic_core* core = get_core(); 533 + 534 + tic_mem* tic = (tic_mem*)core; 502 535 503 536 core->api.memcpy(tic, dest, src, size); 504 - 505 - return 0; 537 + py_assign(py_retval(), py_None()); 538 + return true; 506 539 } 507 540 508 - static int py_memset(pkpy_vm* vm) { 509 - 510 - 511 - int dest; 512 - int value; 513 - int size; 514 - 515 - pkpy_to_int(vm, 0, &dest); 516 - pkpy_to_int(vm, 1, &value); 517 - pkpy_to_int(vm, 2, &size); 518 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 519 - if(pkpy_check_error(vm)) 520 - return 0; 521 - 522 - core->api.memset(tic, dest, value, size); 541 + static bool py_memset(int argc, py_Ref argv) 542 + { 543 + int dest, val, size; 544 + PY_CHECK_ARG_TYPE(0, tp_int); 545 + PY_CHECK_ARG_TYPE(1, tp_int); 546 + PY_CHECK_ARG_TYPE(2, tp_int); 547 + dest = py_toint(py_arg(0)); 548 + val = py_toint(py_arg(1)); 549 + size = py_toint(py_arg(2)); 550 + tic_core* core = get_core(); 551 + 552 + tic_mem* tic = (tic_mem*)core; 523 553 524 - return 0; 554 + core->api.memset(tic, dest, val, size); 555 + py_assign(py_retval(), py_None()); 556 + return true; 525 557 } 526 558 527 - static int py_mget(pkpy_vm* vm) { 559 + static bool py_mget(int argc, py_Ref argv) 560 + { 561 + int x, y; 562 + PY_CHECK_ARG_TYPE(0, tp_int); 563 + PY_CHECK_ARG_TYPE(1, tp_int); 564 + x = py_toint(py_arg(0)); 565 + y = py_toint(py_arg(1)); 566 + tic_core* core = get_core(); 567 + 568 + tic_mem* tic = (tic_mem*)core; 528 569 529 - 530 - int x; 531 - int y; 532 - 533 - pkpy_to_int(vm, 0, &x); 534 - pkpy_to_int(vm, 1, &y); 535 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 536 - if(pkpy_check_error(vm)) 537 - return 0; 538 - 539 - int value = core->api.mget(tic, x, y); 540 - pkpy_push_int(vm, value); 541 - 542 - return 1; 570 + int res = core->api.mget(tic, x, y); 571 + py_newint(py_retval(), res); 572 + return true; 543 573 } 544 574 545 - static int py_mset(pkpy_vm* vm) { 546 - 547 - 548 - int x; 549 - int y; 550 - int tile_id; 551 - 552 - pkpy_to_int(vm, 0, &x); 553 - pkpy_to_int(vm, 1, &y); 554 - pkpy_to_int(vm, 2, &tile_id); 555 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 556 - if(pkpy_check_error(vm)) 557 - return 0; 558 - 559 - core->api.mset(tic, x, y, tile_id); 575 + static bool py_mset(int argc, py_Ref argv) 576 + { 577 + int x, y, title_id; 578 + PY_CHECK_ARG_TYPE(0, tp_int); 579 + PY_CHECK_ARG_TYPE(1, tp_int); 580 + PY_CHECK_ARG_TYPE(2, tp_int); 581 + x = py_toint(py_arg(0)); 582 + y = py_toint(py_arg(1)); 583 + title_id = py_toint(py_arg(2)); 584 + tic_core* core = get_core(); 585 + 586 + tic_mem* tic = (tic_mem*)core; 560 587 561 - return 0; 588 + core->api.mset(tic, x, y, title_id); 589 + py_assign(py_retval(), py_None()); 590 + return true; 562 591 } 563 592 564 - 565 - static int py_mouse(pkpy_vm* vm) { 566 - 567 - tic_core* core; 568 - get_core(vm, &core); 569 - if(pkpy_check_error(vm)) 570 - return 0; 593 + static bool py_mouse(int argc, py_Ref argv) 594 + { 595 + tic_core* core = get_core(); 596 + 597 + tic_mem* tic = (tic_mem*)core; 571 598 572 - tic_point pos = core->api.mouse((tic_mem*)core); 573 - 599 + tic_point pos = core->api.mouse(tic); 574 600 const tic80_mouse* mouse = &core->memory.ram->input.mouse; 575 601 576 - pkpy_push_int(vm, pos.x); 577 - pkpy_push_int(vm, pos.y); 578 - pkpy_push_bool(vm, mouse->left); 579 - pkpy_push_bool(vm, mouse->middle); 580 - pkpy_push_bool(vm, mouse->right); 581 - pkpy_push_int(vm, mouse->scrollx); 582 - pkpy_push_int(vm, mouse->scrolly); 583 - 584 - return 7; 602 + //py_Ref res = (py_Ref)malloc(sizeof(py_Ref)); 603 + py_Ref res = py_r7(); 604 + py_newtuple(res, 7); 605 + py_newint(py_tuple_getitem(res, 0), pos.x); 606 + py_newint(py_tuple_getitem(res, 1), pos.y); 607 + py_newint(py_tuple_getitem(res, 2), mouse->left); 608 + py_newint(py_tuple_getitem(res, 3), mouse->middle); 609 + py_newint(py_tuple_getitem(res, 4), mouse->right); 610 + py_newint(py_tuple_getitem(res, 5), mouse->scrollx); 611 + py_newint(py_tuple_getitem(res, 6), mouse->scrolly); 612 + py_assign(py_retval(), res); 613 + return true; 585 614 } 586 615 587 - static int py_music(pkpy_vm* vm) { 616 + static bool py_music(int argc, py_Ref argv) 617 + { 618 + int track, frame, row, tempo, speed; 619 + bool loop, sustain; 620 + PY_CHECK_ARG_TYPE(0, tp_int); 621 + PY_CHECK_ARG_TYPE(1, tp_int); 622 + PY_CHECK_ARG_TYPE(2, tp_int); 623 + PY_CHECK_ARG_TYPE(3, tp_bool); 624 + PY_CHECK_ARG_TYPE(4, tp_bool); 625 + PY_CHECK_ARG_TYPE(5, tp_int); 626 + PY_CHECK_ARG_TYPE(6, tp_int); 627 + track = py_toint(py_arg(0)); 628 + frame = py_toint(py_arg(1)); 629 + row = py_toint(py_arg(2)); 630 + loop = py_tobool(py_arg(3)); 631 + sustain = py_tobool(py_arg(4)); 632 + tempo = py_toint(py_arg(5)); 633 + speed = py_toint(py_arg(6)); 634 + tic_core* core = get_core(); 635 + 636 + tic_mem* tic = (tic_mem*)core; 588 637 589 - 590 - int track; 591 - int frame; 592 - int row; 593 - bool loop; 594 - bool sustain; 595 - int tempo; 596 - int speed; 597 - 598 - pkpy_to_int(vm, 0, &track); 599 - pkpy_to_int(vm, 1, &frame); 600 - pkpy_to_int(vm, 2, &row); 601 - pkpy_to_bool(vm, 3, &loop); 602 - pkpy_to_bool(vm, 4, &sustain); 603 - pkpy_to_int(vm, 5, &tempo); 604 - pkpy_to_int(vm, 6, &speed); 605 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 606 - if(pkpy_check_error(vm)) 607 - return 0; 608 - 609 - if (track > MUSIC_TRACKS - 1 ) 610 - pkpy_error(vm, "RuntimeError", pkpy_string("invalid music track index\n")); 611 - 612 - //stop the music first I guess 638 + if (track > MUSIC_TRACKS - 1) 639 + { 640 + return ValueError("invalid music track number"); 641 + } 613 642 core->api.music(tic, -1, 0, 0, false, false, -1, -1); 614 - if (track >= 0) 615 - core->api.music(tic, track, frame, row, loop, sustain, tempo, speed); 616 - 617 - return 0; 643 + core->api.music(tic, track, frame, row, loop, sustain, tempo, speed); 644 + py_assign(py_retval(), py_None()); 645 + return true; 618 646 } 619 647 620 - static int py_peek(pkpy_vm* vm) { 621 - 622 - 623 - int address; 624 - int bits; 625 - 626 - pkpy_to_int(vm, 0, &address); 627 - pkpy_to_int(vm, 1, &bits); 628 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 629 - if(pkpy_check_error(vm)) 630 - return 0; 631 - 632 - int value = core->api.peek(tic, address, bits); 633 - pkpy_push_int(vm, value); 648 + static bool pyy_peek(int argc, py_Ref argv) 649 + { 650 + int addr, bits; 651 + PY_CHECK_ARG_TYPE(0, tp_int); 652 + PY_CHECK_ARG_TYPE(1, tp_int); 653 + addr = py_toint(py_arg(0)); 654 + bits = py_toint(py_arg(1)); 655 + tic_core* core = get_core(); 656 + 657 + tic_mem* tic = (tic_mem*)core; 634 658 635 - return 1; 659 + int res = core->api.peek(tic, addr, bits); 660 + py_newint(py_retval(), res); 661 + return true; 636 662 } 637 663 638 - static int py_peek1(pkpy_vm* vm) { 639 - 640 - 641 - int address; 642 - 643 - pkpy_to_int(vm, 0, &address); 644 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 645 - if(pkpy_check_error(vm)) 646 - return 0; 647 - 648 - int value = core->api.peek1(tic, address); 649 - pkpy_push_int(vm, value); 664 + static bool py_peek1(int argc, py_Ref argv) 665 + { 666 + int addr; 667 + PY_CHECK_ARG_TYPE(0, tp_int); 668 + addr = py_toint(py_arg(0)); 669 + tic_core* core = get_core(); 670 + 671 + tic_mem* tic = (tic_mem*)core; 650 672 651 - return 1; 673 + int res = core->api.peek1(tic, addr); 674 + py_newint(py_retval(), res); 675 + return true; 652 676 } 653 677 654 - static int py_peek2(pkpy_vm* vm) { 655 - 656 - 657 - int address; 658 - 659 - pkpy_to_int(vm, 0, &address); 660 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 661 - if(pkpy_check_error(vm)) 662 - return 0; 663 - 664 - int value = core->api.peek2(tic, address); 665 - pkpy_push_int(vm, value); 678 + static bool py_peek2(int argc, py_Ref argv) 679 + { 680 + int addr; 681 + PY_CHECK_ARG_TYPE(0, tp_int); 682 + addr = py_toint(py_arg(0)); 683 + tic_core* core = get_core(); 684 + 685 + tic_mem* tic = (tic_mem*)core; 666 686 667 - return 1; 687 + int res = core->api.peek2(tic, addr); 688 + py_newint(py_retval(), res); 689 + return true; 668 690 } 669 691 670 - static int py_peek4(pkpy_vm* vm) { 692 + static bool py_peek4(int argc, py_Ref argv) 693 + { 694 + int addr; 695 + PY_CHECK_ARG_TYPE(0, tp_int); 696 + addr = py_toint(py_arg(0)); 697 + tic_core* core = get_core(); 698 + 699 + tic_mem* tic = (tic_mem*)core; 671 700 672 - 673 - int address; 674 - 675 - pkpy_to_int(vm, 0, &address); 676 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 677 - if(pkpy_check_error(vm)) 678 - return 0; 679 - 680 - int value = core->api.peek4(tic, address); 681 - pkpy_push_int(vm, value); 682 - 683 - return 1; 701 + int res = core->api.peek4(tic, addr); 702 + py_newint(py_retval(), res); 703 + return true; 684 704 } 685 705 686 - static int py_pix(pkpy_vm* vm) { 706 + static bool py_pix(int argc, py_Ref argv) 707 + { 708 + int x, y, color; 709 + color = -1; 710 + PY_CHECK_ARG_TYPE(0, tp_int); 711 + PY_CHECK_ARG_TYPE(1, tp_int); 712 + if (!py_isnone(py_arg(2))) color = py_toint(py_arg(2)); 713 + x = py_toint(py_arg(0)); 714 + y = py_toint(py_arg(1)); 715 + tic_core* core = get_core(); 716 + 717 + tic_mem* tic = (tic_mem*)core; 687 718 688 - int x; 689 - int y; 690 - int color = -1; 691 - 692 - pkpy_to_int(vm, 0, &x); 693 - pkpy_to_int(vm, 1, &y); 694 - if(!pkpy_is_none(vm, 2)) pkpy_to_int(vm, 2, &color); 695 - 696 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 697 - 698 - if(pkpy_check_error(vm)) 699 - return 0; 700 - 701 - if(color >= 0) { //set the pixel 719 + if (color >= 0) //set pixel 720 + { 702 721 core->api.pix(tic, x, y, color, false); 703 - return 0; 704 - } else { //get the pixel 705 - int value = core->api.pix(tic, x, y, 0, true); 706 - pkpy_push_int(vm, value); 707 - return 1; 722 + py_assign(py_retval(), py_None()); 723 + return true; 724 + } 725 + else //get pixel to retval 726 + { 727 + int res = core->api.pix(tic, x, y, 0, true); 728 + py_newint(py_retval(), res); 729 + return true; 708 730 } 709 731 } 710 732 711 - static int py_pmem(pkpy_vm* vm) { 712 - 713 - int index; 714 - bool provided_value = false; 715 - int value; 716 - 717 - pkpy_to_int(vm, 0, &index); 718 - if (!pkpy_is_none(vm, 1)) { 719 - provided_value = true; 720 - pkpy_to_int(vm, 1, &value); 733 + static bool py_pmem(int argc, py_Ref argv) 734 + { 735 + int index, val; 736 + bool has_val = false; 737 + PY_CHECK_ARG_TYPE(0, tp_int); 738 + if (!py_isnone(py_arg(1))) 739 + { 740 + has_val = true; 741 + PY_CHECK_ARG_TYPE(1, tp_int); 742 + val = py_toint(py_arg(1)); 721 743 } 722 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 723 - if(pkpy_check_error(vm)) 724 - return 0; 744 + index = py_toint(py_arg(0)); 745 + tic_core* core = get_core(); 746 + 747 + tic_mem* tic = (tic_mem*)core; 725 748 726 - if (index >= TIC_PERSISTENT_SIZE) { 727 - pkpy_error(vm, "RuntimeError", pkpy_string("invalid persistent tic index\n")); 728 - return 0; 749 + if (index >= TIC_PERSISTENT_SIZE) 750 + { 751 + return ValueError("invalid tic persistent index"); 729 752 } 730 753 731 - int stored = core->api.pmem(tic, index, 0, false); 732 - pkpy_push_int(vm, stored); 733 - 734 - if(provided_value) //set the value 735 - core->api.pmem(tic, index, value, true); 736 - 737 - return 1; 738 - } 739 - 740 - static int py_poke(pkpy_vm* vm) { 741 - 742 - 743 - int address; 744 - int value; 745 - int bits; 746 - 747 - pkpy_to_int(vm, 0, &address); 748 - pkpy_to_int(vm, 1, &value); 749 - pkpy_to_int(vm, 2, &bits); 750 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 751 - if(pkpy_check_error(vm)) 752 - return 0; 754 + int res = core->api.pmem(tic, index, 0, false); 755 + py_newint(py_retval(), res); 753 756 754 - core->api.poke(tic, address, value, bits); 755 - 756 - return 0; 757 + if (has_val) 758 + { 759 + core->api.pmem(tic, index, val, true); 760 + py_assign(py_retval(), py_None()); 761 + } 762 + return true; 757 763 } 758 764 759 - static int py_poke1(pkpy_vm* vm) { 760 - 761 - 762 - int address; 763 - int value; 764 - 765 - pkpy_to_int(vm, 0, &address); 766 - pkpy_to_int(vm, 1, &value); 767 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 768 - if(pkpy_check_error(vm)) 769 - return 0; 770 - 771 - core->api.poke1(tic, address, value); 765 + static bool py_poke(int argc, py_Ref argv) 766 + { 767 + int addr, val, bits; 768 + PY_CHECK_ARG_TYPE(0, tp_int); 769 + PY_CHECK_ARG_TYPE(1, tp_int); 770 + PY_CHECK_ARG_TYPE(2, tp_int); 771 + addr = py_toint(py_arg(0)); 772 + val = py_toint(py_arg(1)); 773 + bits = py_toint(py_arg(2)); 774 + tic_core* core = get_core(); 775 + 776 + tic_mem* tic = (tic_mem*)core; 772 777 773 - return 0; 778 + core->api.poke(tic, addr, val, bits); 779 + py_assign(py_retval(), py_None()); 780 + return true; 774 781 } 775 782 776 - static int py_poke2(pkpy_vm* vm) { 777 - 778 - 779 - int address; 780 - int value; 783 + static bool py_poke1(int argc, py_Ref argv) 784 + { 785 + int addr, val; 786 + PY_CHECK_ARG_TYPE(0, tp_int); 787 + PY_CHECK_ARG_TYPE(1, tp_int); 788 + addr = py_toint(py_arg(0)); 789 + val = py_toint(py_arg(1)); 790 + tic_core* core = get_core(); 791 + 792 + tic_mem* tic = (tic_mem*)core; 781 793 782 - pkpy_to_int(vm, 0, &address); 783 - pkpy_to_int(vm, 1, &value); 784 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 785 - if(pkpy_check_error(vm)) 786 - return 0; 787 - 788 - core->api.poke2(tic, address, value); 789 - 790 - return 0; 794 + core->api.poke1(tic, addr, val); 795 + py_assign(py_retval(), py_None()); 796 + return true; 791 797 } 792 798 793 - static int py_poke4(pkpy_vm* vm) { 794 - 795 - 796 - int address; 797 - int value; 798 - 799 - pkpy_to_int(vm, 0, &address); 800 - pkpy_to_int(vm, 1, &value); 801 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 802 - if(pkpy_check_error(vm)) 803 - return 0; 804 - 805 - core->api.poke4(tic, address, value); 799 + static bool py_poke2(int argc, py_Ref argv) 800 + { 801 + int addr, val; 802 + PY_CHECK_ARG_TYPE(0, tp_int); 803 + PY_CHECK_ARG_TYPE(1, tp_int); 804 + addr = py_toint(py_arg(0)); 805 + val = py_toint(py_arg(1)); 806 + tic_core* core = get_core(); 807 + 808 + tic_mem* tic = (tic_mem*)core; 806 809 807 - return 0; 810 + core->api.poke2(tic, addr, val); 811 + py_assign(py_retval(), py_None()); 812 + return true; 808 813 } 809 814 810 - static int py_print(pkpy_vm* vm) { 811 - 812 - 813 - pkpy_CString text; 814 - int x; 815 - int y; 816 - int color; 817 - bool fixed; 818 - int scale; 819 - bool alt; 815 + static bool py_poke4(int argc, py_Ref argv) 816 + { 817 + int addr, val; 818 + PY_CHECK_ARG_TYPE(0, tp_int); 819 + PY_CHECK_ARG_TYPE(1, tp_int); 820 + addr = py_toint(py_arg(0)); 821 + val = py_toint(py_arg(1)); 822 + tic_core* core = get_core(); 823 + 824 + tic_mem* tic = (tic_mem*)core; 820 825 821 - pkpy_dup(vm, 0); 822 - pkpy_py_str(vm); 823 - pkpy_to_string(vm, -1, &text); 824 - pkpy_pop_top(vm); 825 - 826 - pkpy_to_int(vm, 1, &x); 827 - pkpy_to_int(vm, 2, &y); 828 - pkpy_to_int(vm, 3, &color); 829 - pkpy_to_bool(vm, 4, &fixed); 830 - pkpy_to_int(vm, 5, &scale); 831 - pkpy_to_bool(vm, 6, &alt); 832 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 833 - if(pkpy_check_error(vm)) { 834 - return 0; 835 - } 836 - 837 - s32 size = core->api.print(tic, text, x, y, color, fixed, scale, alt); 838 - pkpy_push_int(vm, size); 839 - return 1; 826 + core->api.poke4(tic, addr, val); 827 + py_assign(py_retval(), py_None()); 828 + return true; 840 829 } 841 830 842 - static int py_rect(pkpy_vm* vm) 831 + static bool py_rect(int argc, py_Ref argv) 843 832 { 844 - 845 - int x; 846 - int y; 847 - int w; 848 - int h; 849 - int color; 850 - 851 - pkpy_to_int(vm, 0, &x); 852 - pkpy_to_int(vm, 1, &y); 853 - pkpy_to_int(vm, 2, &w); 854 - pkpy_to_int(vm, 3, &h); 855 - pkpy_to_int(vm, 4, &color); 856 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 857 - if(pkpy_check_error(vm)) 858 - return 0; 833 + int x, y, w, h, color; 834 + PY_CHECK_ARG_TYPE(0, tp_int); 835 + PY_CHECK_ARG_TYPE(1, tp_int); 836 + PY_CHECK_ARG_TYPE(2, tp_int); 837 + PY_CHECK_ARG_TYPE(3, tp_int); 838 + PY_CHECK_ARG_TYPE(4, tp_int); 839 + x = py_toint(py_arg(0)); 840 + y = py_toint(py_arg(1)); 841 + w = py_toint(py_arg(2)); 842 + h = py_toint(py_arg(3)); 843 + color = py_toint(py_arg(4)); 844 + tic_core* core = get_core(); 845 + 846 + tic_mem* tic = (tic_mem*)core; 859 847 860 848 core->api.rect(tic, x, y, w, h, color); 861 - return 0; 849 + py_assign(py_retval(), py_None()); 850 + return true; 862 851 } 863 852 864 - static int py_rectb(pkpy_vm* vm) 853 + static bool py_rectb(int argc, py_Ref argv) 865 854 { 866 - 867 - int x; 868 - int y; 869 - int w; 870 - int h; 871 - int color; 872 - 873 - pkpy_to_int(vm, 0, &x); 874 - pkpy_to_int(vm, 1, &y); 875 - pkpy_to_int(vm, 2, &w); 876 - pkpy_to_int(vm, 3, &h); 877 - pkpy_to_int(vm, 4, &color); 878 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 879 - if(pkpy_check_error(vm)) 880 - return 0; 855 + int x, y, w, h, color; 856 + PY_CHECK_ARG_TYPE(0, tp_int); 857 + PY_CHECK_ARG_TYPE(1, tp_int); 858 + PY_CHECK_ARG_TYPE(2, tp_int); 859 + PY_CHECK_ARG_TYPE(3, tp_int); 860 + PY_CHECK_ARG_TYPE(4, tp_int); 861 + x = py_toint(py_arg(0)); 862 + y = py_toint(py_arg(1)); 863 + w = py_toint(py_arg(2)); 864 + h = py_toint(py_arg(3)); 865 + color = py_toint(py_arg(4)); 866 + tic_core* core = get_core(); 867 + 868 + tic_mem* tic = (tic_mem*)core; 881 869 882 870 core->api.rectb(tic, x, y, w, h, color); 883 - return 0; 871 + py_assign(py_retval(), py_None()); 872 + return true; 884 873 } 885 874 886 - static int py_sfx(pkpy_vm* vm) 875 + static bool py_reset(int argc, py_Ref argv) 887 876 { 888 - 889 - int sfx_id; 890 - 891 - bool parse_note_flag = false; 892 - const char* string_note = NULL; 893 - int int_note; 894 - 895 - int duration; 896 - int channel; 897 - int volume; 898 - int speed; 877 + tic_core* core = get_core(); 878 + 879 + tic_mem* tic = (tic_mem*)core; 899 880 900 - pkpy_to_int(vm, 0, &sfx_id); 881 + core->api.reset(tic); 882 + py_assign(py_retval(), py_None()); 883 + return true; 884 + } 901 885 902 - if (pkpy_is_string(vm, 1)) { 903 - parse_note_flag = true; 904 - pkpy_to_string(vm, 1, &string_note); 905 - } else { 906 - pkpy_to_int(vm, 1, &int_note); 886 + static bool py_sfx(int argc, py_Ref argv) 887 + { 888 + int id, _note, duration, channel, volume, speed; 889 + bool _parse_note = false; 890 + const char* str_note; 891 + PY_CHECK_ARG_TYPE(0, tp_int); 892 + id = py_toint(py_arg(0)); 893 + if (py_isstr(py_arg(1))) 894 + { 895 + _parse_note = true; 896 + str_note = py_tostr(py_arg(1)); 897 + } 898 + else 899 + { 900 + PY_CHECK_ARG_TYPE(1, tp_int); 901 + _note = py_toint(py_arg(1)); 907 902 } 903 + PY_CHECK_ARG_TYPE(2, tp_int); 904 + PY_CHECK_ARG_TYPE(3, tp_int); 905 + PY_CHECK_ARG_TYPE(4, tp_int); 906 + PY_CHECK_ARG_TYPE(5, tp_int); 907 + duration = py_toint(py_arg(2)); 908 + channel = py_toint(py_arg(3)); 909 + volume = py_toint(py_arg(4)); 910 + speed = py_toint(py_arg(5)); 911 + tic_core* core = get_core(); 912 + 913 + tic_mem* tic = (tic_mem*)core; 908 914 909 - pkpy_to_int(vm, 2, &duration); 910 - pkpy_to_int(vm, 3, &channel); 911 - pkpy_to_int(vm, 4, &volume); 912 - pkpy_to_int(vm, 5, &speed); 913 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 914 - if(pkpy_check_error(vm)) 915 - goto cleanup; 916 915 s32 note, octave; 917 - 918 - if (parse_note_flag) { 919 - if(!parse_note(string_note, &note, &octave)) { 920 - pkpy_error(vm, "RuntimeError", pkpy_string("invalid note, should like C#4\n")); 921 - goto cleanup; //error in future; 916 + if (_parse_note) 917 + { 918 + if (!parse_note(str_note, &note, &octave)) 919 + { 920 + return ValueError("invalid note, should be like C#4"); 922 921 } 923 - 924 - } else { 925 - note = int_note % NOTES; 926 - octave = int_note/ NOTES; 927 922 } 928 - 929 - if (channel < 0 || channel >= TIC_SOUND_CHANNELS) { 930 - pkpy_error(vm, "RuntimeError", pkpy_string("unknown channel\n")); 931 - goto cleanup; 923 + else 924 + { 925 + note = _note % NOTES; 926 + octave = _note / NOTES; 932 927 } 933 928 934 - if (sfx_id >= SFX_COUNT) { 935 - pkpy_error(vm, "RuntimeError", pkpy_string("unknown sfx index\n")); 936 - goto cleanup; 929 + if (channel < 0 || channel >= TIC_SOUND_CHANNELS) 930 + { 931 + return ValueError("invalid channel"); 937 932 } 938 933 939 - 940 - //for now we won't support two channel volumes 941 - core->api.sfx(tic, sfx_id, note, octave, duration, channel, volume & 0xf, volume & 0xf, speed); 934 + if (id >= SFX_COUNT) 935 + { 936 + return ValueError("invalid sfx index"); 937 + } 942 938 943 - cleanup : 944 - return 0; 939 + core->api.sfx(tic, id, note, octave, duration, channel, volume & 0xf, volume & 0xf, speed); 940 + py_assign(py_retval(), py_None()); 941 + return true; 945 942 } 946 943 947 - static int py_spr(pkpy_vm* vm) 944 + static bool py_sync(int argc, py_Ref argv) 948 945 { 949 - 950 - int spr_id; 951 - int x; 952 - int y; 953 - int color_count; 954 - int scale; 955 - int flip; 956 - int rotate; 957 - int w; 958 - int h; 959 - 960 - static u8 colors[TIC_PALETTE_SIZE]; 961 - 962 - pkpy_to_int(vm, 0, &spr_id); 963 - pkpy_to_int(vm, 1, &x); 964 - pkpy_to_int(vm, 2, &y); 965 - color_count = prepare_colorindex(vm, 3, colors); 966 - pkpy_to_int(vm, 4, &scale); 967 - pkpy_to_int(vm, 5, &flip); 968 - pkpy_to_int(vm, 6, &rotate); 969 - pkpy_to_int(vm, 7, &w); 970 - pkpy_to_int(vm, 8, &h); 971 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 972 - if(pkpy_check_error(vm)) 973 - return 0; 974 - 975 - core->api.spr(tic, spr_id, x, y, w, h, colors, color_count, scale, flip, rotate); 976 - 977 - return 0; 978 - } 979 - 980 - static int py_reset(pkpy_vm* vm) { 981 - tic_core* core; 982 - get_core(vm, &core); 983 - if(pkpy_check_error(vm)) 984 - return 0; 985 - 986 - core->state.initialized = false; 987 - 988 - return 0; 989 - } 990 - 991 - static int py_sync(pkpy_vm* vm) 992 - { 993 - 994 - int mask; 995 - int bank; 946 + int mask, bank; 996 947 bool tocart; 997 - 998 - pkpy_to_int(vm, 0, &mask); 999 - pkpy_to_int(vm, 1, &bank); 1000 - pkpy_to_bool(vm, 2, &tocart); 1001 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 1002 - if(pkpy_check_error(vm)) 1003 - return 0; 948 + PY_CHECK_ARG_TYPE(0, tp_int); 949 + PY_CHECK_ARG_TYPE(1, tp_int); 950 + PY_CHECK_ARG_TYPE(2, tp_bool); 951 + mask = py_toint(py_arg(0)); 952 + bank = py_toint(py_arg(1)); 953 + tocart = py_tobool(py_arg(2)); 954 + tic_core* core = get_core(); 955 + 956 + tic_mem* tic = (tic_mem*)core; 1004 957 1005 - if (bank < 0 || bank >= TIC_BANKS) { 1006 - pkpy_error(vm, "RuntimeError", pkpy_string("sync() error, invalid bank\n")); 1007 - return 0; 958 + if (bank < 0 || bank >= TIC_BANKS) 959 + { 960 + return ValueError("invalid sync bank"); 1008 961 } 1009 962 1010 - 1011 963 core->api.sync(tic, mask, bank, tocart); 1012 - return 0; 964 + py_assign(py_retval(), py_None()); 965 + return true; 1013 966 } 1014 967 1015 - static int py_time(pkpy_vm* vm) 968 + static bool py_ttri(int argc, py_Ref argv) 1016 969 { 970 + double x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3; 971 + int texsrc, chromakey; 972 + double z1, z2, z3; 973 + char colors[16]; 974 + PY_CHECK_ARG_TYPE(12, tp_int); 975 + if (py_istype(py_arg(13), tp_int) || py_istype(py_arg(13), tp_list)) 976 + { //it's one of the types 977 + chromakey = prepare_colorindex(py_arg(13), colors); 978 + } 979 + else 980 + return TypeError("The given argument is not int or list"); 1017 981 1018 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 1019 - if(pkpy_check_error(vm)) 1020 - return 0; 982 + texsrc = py_toint(py_arg(12)); 983 + if (!py_castfloat(py_arg(0), &x1)) return false; 984 + if (!py_castfloat(py_arg(1), &y1)) return false; 985 + if (!py_castfloat(py_arg(2), &x2)) return false; 986 + if (!py_castfloat(py_arg(3), &y2)) return false; 987 + if (!py_castfloat(py_arg(4), &x3)) return false; 988 + if (!py_castfloat(py_arg(5), &y3)) return false; 989 + if (!py_castfloat(py_arg(6), &u1)) return false; 990 + if (!py_castfloat(py_arg(7), &v1)) return false; 991 + if (!py_castfloat(py_arg(8), &u2)) return false; 992 + if (!py_castfloat(py_arg(9), &v2)) return false; 993 + if (!py_castfloat(py_arg(10), &u3)) return false; 994 + if (!py_castfloat(py_arg(11), &v3)) return false; 995 + if (!py_castfloat(py_arg(14), &z1)) return false; 996 + if (!py_castfloat(py_arg(15), &z2)) return false; 997 + if (!py_castfloat(py_arg(16), &z3)) return false; 998 + tic_core* core = get_core(); 999 + 1000 + tic_mem* tic = (tic_mem*)core; 1021 1001 1022 - double time = core->api.time(tic); 1023 - pkpy_push_float(vm, time); 1024 - return 1; 1002 + core->api.ttri(tic, 1003 + x1, y1, x2, y2, x3, y3, u1, v1, u2, v2, u3, v3, 1004 + texsrc, 1005 + colors, chromakey, 1006 + z1, z2, z3, 1007 + z1 != 0 || z2 != 0 || z3 != 0); 1008 + py_assign(py_retval(), py_None()); 1009 + return true; 1025 1010 } 1026 1011 1027 - static int py_tstamp(pkpy_vm* vm) 1012 + static bool py_time(int argc, py_Ref argv) 1028 1013 { 1029 - 1030 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 1031 - if(pkpy_check_error(vm)) 1032 - return 0; 1014 + tic_core* core = get_core(); 1015 + 1016 + tic_mem* tic = (tic_mem*)core; 1033 1017 1034 - int tstamp = core->api.tstamp(tic); 1035 - pkpy_push_int(vm, tstamp); 1036 - return 1; 1018 + double time = core->api.time(tic); 1019 + py_newfloat(py_retval(), time); 1020 + return true; 1037 1021 } 1038 1022 1039 - static int py_tri(pkpy_vm* vm) 1023 + static bool py_trace(int argc, py_Ref argv) 1040 1024 { 1041 - 1042 - double x1; 1043 - double y1; 1044 - double x2; 1045 - double y2; 1046 - double x3; 1047 - double y3; 1048 1025 int color; 1049 - 1050 - pkpy_to_float(vm, 0, &x1); 1051 - pkpy_to_float(vm, 1, &y1); 1052 - pkpy_to_float(vm, 2, &x2); 1053 - pkpy_to_float(vm, 3, &y2); 1054 - pkpy_to_float(vm, 4, &x3); 1055 - pkpy_to_float(vm, 5, &y3); 1056 - pkpy_to_int(vm, 6, &color); 1057 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 1058 - if(pkpy_check_error(vm)) 1059 - return 0; 1026 + const char* msg; 1027 + PY_CHECK_ARG_TYPE(0, tp_str); 1028 + PY_CHECK_ARG_TYPE(1, tp_int); 1029 + msg = py_tostr(py_arg(0)); 1030 + color = py_toint(py_arg(1)); 1031 + tic_core* core = get_core(); 1032 + 1033 + tic_mem* tic = (tic_mem*)core; 1060 1034 1061 - core->api.tri(tic, x1, y1, x2, y2, x3, y3, color); 1062 - return 0; 1035 + core->api.trace(tic, msg, (u8)color); 1036 + py_assign(py_retval(), py_None()); 1037 + return true; 1063 1038 } 1064 1039 1065 - static int py_trib(pkpy_vm* vm) 1040 + static bool py_tri(int argc, py_Ref argv) 1066 1041 { 1067 - 1068 - double x1; 1069 - double y1; 1070 - double x2; 1071 - double y2; 1072 - double x3; 1073 - double y3; 1074 1042 int color; 1043 + double x1, y1, x2, y2, x3, y3; 1044 + PY_CHECK_ARG_TYPE(6, tp_int); 1045 + if (!py_castfloat(py_arg(0), &x1)) return false; 1046 + if (!py_castfloat(py_arg(1), &y1)) return false; 1047 + if (!py_castfloat(py_arg(2), &x2)) return false; 1048 + if (!py_castfloat(py_arg(3), &y2)) return false; 1049 + if (!py_castfloat(py_arg(4), &x3)) return false; 1050 + if (!py_castfloat(py_arg(5), &y3)) return false; 1051 + color = py_toint(py_arg(6)); 1052 + tic_core* core = get_core(); 1053 + 1054 + tic_mem* tic = (tic_mem*)core; 1075 1055 1076 - pkpy_to_float(vm, 0, &x1); 1077 - pkpy_to_float(vm, 1, &y1); 1078 - pkpy_to_float(vm, 2, &x2); 1079 - pkpy_to_float(vm, 3, &y2); 1080 - pkpy_to_float(vm, 4, &x3); 1081 - pkpy_to_float(vm, 5, &y3); 1082 - pkpy_to_int(vm, 6, &color); 1083 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 1084 - if(pkpy_check_error(vm)) 1085 - return 0; 1086 - 1087 - core->api.trib(tic, x1, y1, x2, y2, x3, y3, color); 1088 - return 0; 1056 + core->api.tri(tic, x1, y1, x2, y3, x3, y3, (u8)color); 1057 + py_assign(py_retval(), py_None()); 1058 + return true; 1089 1059 } 1090 1060 1091 - static int py_ttri(pkpy_vm* vm) 1061 + static bool py_trib(int argc, py_Ref argv) 1092 1062 { 1063 + int color; 1064 + double x1, y1, x2, y2, x3, y3; 1065 + PY_CHECK_ARG_TYPE(6, tp_int); 1066 + if (!py_castfloat(py_arg(0), &x1)) return false; 1067 + if (!py_castfloat(py_arg(1), &y1)) return false; 1068 + if (!py_castfloat(py_arg(2), &x2)) return false; 1069 + if (!py_castfloat(py_arg(3), &y2)) return false; 1070 + if (!py_castfloat(py_arg(4), &x3)) return false; 1071 + if (!py_castfloat(py_arg(5), &y3)) return false; 1072 + color = py_toint(py_arg(6)); 1073 + tic_core* core = get_core(); 1074 + 1075 + tic_mem* tic = (tic_mem*)core; 1093 1076 1094 - double x1; 1095 - double y1; 1096 - double x2; 1097 - double y2; 1098 - double x3; 1099 - double y3; 1100 - double u1; 1101 - double v1; 1102 - double u2; 1103 - double v2; 1104 - double u3; 1105 - double v3; 1106 - int texsrc; 1107 - int color_count; 1108 - double z1; 1109 - double z2; 1110 - double z3; 1111 - 1112 - static u8 colors[TIC_PALETTE_SIZE]; 1113 - 1114 - pkpy_to_float(vm, 0, &x1); 1115 - pkpy_to_float(vm, 1, &y1); 1116 - pkpy_to_float(vm, 2, &x2); 1117 - pkpy_to_float(vm, 3, &y2); 1118 - pkpy_to_float(vm, 4, &x3); 1119 - pkpy_to_float(vm, 5, &y3); 1120 - 1121 - pkpy_to_float(vm, 6, &u1); 1122 - pkpy_to_float(vm, 7, &v1); 1123 - pkpy_to_float(vm, 8, &u2); 1124 - pkpy_to_float(vm, 9, &v2); 1125 - pkpy_to_float(vm, 10, &u3); 1126 - pkpy_to_float(vm, 11, &v3); 1127 - 1128 - pkpy_to_int(vm, 12, &texsrc); 1129 - color_count = prepare_colorindex(vm, 13, colors); 1130 - 1131 - pkpy_to_float(vm, 14, &z1); 1132 - pkpy_to_float(vm, 15, &z2); 1133 - pkpy_to_float(vm, 16, &z3); 1134 - 1135 - tic_core* core; get_core(vm, &core); tic_mem* tic = (tic_mem*)core; 1136 - if(pkpy_check_error(vm)) 1137 - return 0; 1138 - 1139 - core->api.ttri( 1140 - tic, 1141 - x1,y1,x2,y2,x3,y3, 1142 - u1,v1,u2,v2,u3,v3, 1143 - texsrc, 1144 - colors,color_count, 1145 - z1,z2,z3, 1146 - z1 != 0 || z2 != 0 || z3 != 0 1147 - ); 1148 - 1149 - return 0; 1150 - } 1151 - 1152 - static int py_vbank(pkpy_vm* vm) { 1153 - tic_core* core; 1154 - 1155 - int bank_id = -1; 1156 - 1157 - if (!pkpy_is_none(vm, 0)) 1158 - pkpy_to_int(vm, 0, &bank_id); 1159 - get_core(vm, &core); 1160 - if(pkpy_check_error(vm)) 1161 - return 0; 1162 - 1163 - tic_mem* tic = (tic_mem*) core; 1164 - 1165 - s32 prev = core->state.vbank.id; 1166 - 1167 - if (bank_id >= 0) 1168 - core->api.vbank(tic, bank_id); 1169 - 1170 - pkpy_push_int(vm, prev); 1171 - return 1; 1077 + core->api.trib(tic, x1, y1, x2, y3, x3, y3, (u8)color); 1078 + py_assign(py_retval(), py_None()); 1079 + return true; 1172 1080 } 1173 1081 1174 - static int py_fft(pkpy_vm* vm) 1082 + static bool py_tstamp(int argc, py_Ref argv) 1175 1083 { 1176 - tic_core* core; 1177 - get_core(vm, &core); 1084 + tic_core* core = get_core(); 1085 + 1178 1086 tic_mem* tic = (tic_mem*)core; 1179 - if (pkpy_check_error(vm)) 1180 - return 0; 1181 1087 1182 - s32 start_freq = -1; 1183 - s32 end_freq = -1; 1184 - 1185 - if (!pkpy_is_none(vm, 0)) 1186 - pkpy_to_int(vm, 0, &start_freq); 1187 - if (!pkpy_is_none(vm, 1)) 1188 - pkpy_to_int(vm, 1, &end_freq); 1189 - pkpy_push_float(vm, core->api.fft(tic, start_freq, end_freq)); 1190 - return 1; 1088 + int res = core->api.tstamp(tic); 1089 + py_newint(py_retval(), res); 1090 + return true; 1191 1091 } 1192 1092 1193 - static int py_ffts(pkpy_vm* vm) 1093 + static bool py_vbank(int argc, py_Ref argv) 1194 1094 { 1195 - tic_core* core; 1196 - get_core(vm, &core); 1095 + int bank = -1; 1096 + if (!py_isnone(py_arg(0))) 1097 + { 1098 + bank = py_toint(py_arg(0)); 1099 + } 1100 + tic_core* core = get_core(); 1101 + 1197 1102 tic_mem* tic = (tic_mem*)core; 1198 - if (pkpy_check_error(vm)) 1199 - return 0; 1200 1103 1201 - s32 start_freq = -1; 1202 - s32 end_freq = -1; 1203 - 1204 - if (!pkpy_is_none(vm, 0)) 1205 - pkpy_to_int(vm, 0, &start_freq); 1206 - if (!pkpy_is_none(vm, 1)) 1207 - pkpy_to_int(vm, 1, &end_freq); 1208 - pkpy_push_float(vm, core->api.ffts(tic, start_freq, end_freq)); 1209 - return 1; 1104 + s32 prev = core->state.vbank.id; 1105 + if (bank >= 0) 1106 + { 1107 + core->api.vbank(tic, bank); 1108 + } 1109 + py_newint(py_retval(), prev); 1110 + return true; 1210 1111 } 1211 1112 1212 - static bool setup_c_bindings(pkpy_vm* vm) { 1213 - pkpy_push_function(vm, "btn(id: int) -> bool", py_btn); 1214 - pkpy_setglobal_2(vm, "btn"); 1215 - pkpy_push_function(vm, "btnp(id: int, hold=-1, period=-1) -> bool", py_btnp); 1216 - pkpy_setglobal_2(vm, "btnp"); 1217 - 1218 - pkpy_push_function(vm, "circ(x: int, y: int, radius: int, color: int)", py_circ); 1219 - pkpy_setglobal_2(vm, "circ"); 1220 - pkpy_push_function(vm, "circb(x: int, y: int, radius: int, color: int)", py_circb); 1221 - pkpy_setglobal_2(vm, "circb"); 1222 - 1223 - pkpy_push_function(vm, "clip(x: int, y: int, width: int, height: int)", py_clip); 1224 - pkpy_setglobal_2(vm, "clip"); 1225 - 1226 - pkpy_push_function(vm, "cls(color=0)", py_cls); 1227 - pkpy_setglobal_2(vm, "cls"); 1228 - 1229 - pkpy_push_function(vm, "elli(x: int, y: int, a: int, b: int, color: int)", py_elli); 1230 - pkpy_setglobal_2(vm, "elli"); 1231 - pkpy_push_function(vm, "ellib(x: int, y: int, a: int, b: int, color: int)", py_ellib); 1232 - pkpy_setglobal_2(vm, "ellib"); 1233 - 1234 - pkpy_push_function(vm, "exit()", py_exit); 1235 - pkpy_setglobal_2(vm, "exit"); 1113 + /*************TIC-80 MISC BEGIN****************/ 1236 1114 1237 - pkpy_push_function(vm, "fget(sprite_id: int, flag: int) -> bool", py_fget); 1238 - pkpy_setglobal_2(vm, "fget"); 1239 - pkpy_push_function(vm, "fset(sprite_id: int, flag: int, b: bool)", py_fset); 1240 - pkpy_setglobal_2(vm, "fset"); 1115 + static bool py_throw_error(tic_core* core, const char* msg) 1116 + { 1117 + if (msg != NULL)//reserved for TIC, BOOT et cetra 1118 + { 1119 + core->data->error(core->data->data, msg); 1120 + } 1121 + else 1122 + { 1123 + core->data->error(core->data->data, py_formatexc()); 1124 + } 1125 + return false; 1126 + } 1241 1127 1242 - pkpy_push_function(vm, "font(text: str, x: int, y: int, chromakey: int, char_width=8, char_height=8, fixed=False, scale=1, alt=False) -> int", py_font); 1243 - pkpy_setglobal_2(vm, "font"); 1244 - 1245 - pkpy_push_function(vm, "key(code=-1) -> bool", py_key); 1246 - pkpy_setglobal_2(vm, "key"); 1247 - pkpy_push_function(vm, "keyp(code=-1, hold=-1, period=-17) -> int", py_keyp); 1248 - pkpy_setglobal_2(vm, "keyp"); 1249 - 1250 - pkpy_push_function(vm, "line(x0: int, y0: int, x1: int, y1: int, color: int)", py_line); 1251 - pkpy_setglobal_2(vm, "line"); 1252 - 1253 - pkpy_push_function(vm, "map(x=0, y=0, w=30, h=17, sx=0, sy=0, colorkey=-1, scale=1, remap=None)", py_map); 1254 - pkpy_setglobal_2(vm, "map"); 1255 - 1256 - pkpy_push_function(vm, "memcpy(dest: int, source: int, size: int)", py_memcpy); 1257 - pkpy_setglobal_2(vm, "memcpy"); 1258 - pkpy_push_function(vm, "memset(dest: int, value: int, size: int)", py_memset); 1259 - pkpy_setglobal_2(vm, "memset"); 1260 - 1261 - pkpy_push_function(vm, "mget(x: int, y: int) -> int", py_mget); 1262 - pkpy_setglobal_2(vm, "mget"); 1263 - pkpy_push_function(vm, "mset(x: int, y: int, tile_id: int)", py_mset); 1264 - pkpy_setglobal_2(vm, "mset"); 1265 - 1266 - pkpy_push_function(vm, "mouse() -> tuple[int, int, bool, bool, bool, int, int]", py_mouse); 1267 - pkpy_setglobal_2(vm, "mouse"); 1268 - 1269 - pkpy_push_function(vm, "music(track=-1, frame=-1, row=-1, loop=True, sustain=False, tempo=-1, speed=-1)", py_music); 1270 - pkpy_setglobal_2(vm, "music"); 1271 - 1272 - pkpy_push_function(vm, "paint(x: int, y: int, color: int, bordercolor=-1)", py_paint); 1273 - pkpy_setglobal_2(vm, "paint"); 1274 - 1275 - pkpy_push_function(vm, "peek(addr: int, bits=8) -> int", py_peek); 1276 - pkpy_setglobal_2(vm, "peek"); 1277 - pkpy_push_function(vm, "peek1(addr: int) -> int", py_peek1); 1278 - pkpy_setglobal_2(vm, "peek1"); 1279 - pkpy_push_function(vm, "peek2(addr: int) -> int", py_peek2); 1280 - pkpy_setglobal_2(vm, "peek2"); 1281 - pkpy_push_function(vm, "peek4(addr: int) -> int", py_peek4); 1282 - pkpy_setglobal_2(vm, "peek4"); 1283 - 1284 - pkpy_push_function(vm, "pix(x: int, y: int, color: int=None) -> int | None", py_pix); 1285 - pkpy_setglobal_2(vm, "pix"); 1286 - 1287 - pkpy_push_function(vm, "pmem(index: int, value: int=None) -> int", py_pmem); 1288 - pkpy_setglobal_2(vm, "pmem"); 1289 - 1290 - pkpy_push_function(vm, "poke(addr: int, value: int, bits=8)", py_poke); 1291 - pkpy_setglobal_2(vm, "poke"); 1292 - pkpy_push_function(vm, "poke1(addr: int, value: int)", py_poke1); 1293 - pkpy_setglobal_2(vm, "poke1"); 1294 - pkpy_push_function(vm, "poke2(addr: int, value: int)", py_poke2); 1295 - pkpy_setglobal_2(vm, "poke2"); 1296 - pkpy_push_function(vm, "poke4(addr: int, value: int)", py_poke4); 1297 - pkpy_setglobal_2(vm, "poke4"); 1298 - 1299 - pkpy_push_function(vm, "print(text, x=0, y=0, color=15, fixed=False, scale=1, alt=False)", py_print); 1300 - pkpy_setglobal_2(vm, "print"); 1301 - 1302 - pkpy_push_function(vm, "rect(x: int, y: int, w: int, h: int, color: int)", py_rect); 1303 - pkpy_setglobal_2(vm, "rect"); 1304 - pkpy_push_function(vm, "rectb(x: int, y: int, w: int, h: int, color: int)", py_rectb); 1305 - pkpy_setglobal_2(vm, "rectb"); 1306 - 1307 - pkpy_push_function(vm, "reset()", py_reset); 1308 - pkpy_setglobal_2(vm, "reset"); 1309 - 1310 - pkpy_push_function(vm, "sfx(id: int, note=-1, duration=-1, channel=0, volume=15, speed=0)", py_sfx); 1311 - pkpy_setglobal_2(vm, "sfx"); 1312 - 1313 - pkpy_push_function(vm, "spr(id: int, x: int, y: int, colorkey=-1, scale=1, flip=0, rotate=0, w=1, h=1)", py_spr); 1314 - pkpy_setglobal_2(vm, "spr"); 1315 - 1316 - pkpy_push_function(vm, "sync(mask=0, bank=0, tocart=False)", py_sync); 1317 - pkpy_setglobal_2(vm, "sync"); 1318 - 1319 - pkpy_push_function(vm, "ttri(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float, u1: float, v1: float, u2: float, v2: float, u3: float, v3: float, texsrc=0, chromakey=-1, z1=0.0, z2=0.0, z3=0.0)", py_ttri); 1320 - pkpy_setglobal_2(vm, "ttri"); 1321 - 1322 - pkpy_push_function(vm, "time() -> int", py_time); 1323 - pkpy_setglobal_2(vm, "time"); 1324 - 1325 - pkpy_push_function(vm, "trace(message, color=15)", py_trace); 1326 - pkpy_setglobal_2(vm, "trace"); 1327 - 1328 - pkpy_push_function(vm, "tri(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float, color: int)", py_tri); 1329 - pkpy_setglobal_2(vm, "tri"); 1330 - pkpy_push_function(vm, "trib(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float, color: int)", py_trib); 1331 - pkpy_setglobal_2(vm, "trib"); 1332 - 1333 - pkpy_push_function(vm, "tstamp() -> int", py_tstamp); 1334 - pkpy_setglobal_2(vm, "tstamp"); 1335 - 1336 - pkpy_push_function(vm, "vbank(bank: int=None) -> int", py_vbank); 1337 - pkpy_setglobal_2(vm, "vbank"); 1338 - 1339 - pkpy_push_function(vm, "fft(start_freq: int, end_freq: int=-1) -> float", py_fft); 1340 - pkpy_setglobal_2(vm, "fft"); 1341 - 1342 - pkpy_push_function(vm, "ffts(start_freq: int, end_freq: int=-1) -> float", py_ffts); 1343 - pkpy_setglobal_2(vm, "ffts"); 1344 - 1345 - if(pkpy_check_error(vm)) 1346 - return false; 1347 - 1128 + static bool bind_pkpy_v2() 1129 + { 1130 + py_GlobalRef mod = py_getmodule("__main__"); 1131 + py_bind(mod, "btn(id: int) -> bool", py_btn); 1132 + py_bind(mod, "btnp(id: int, hold=-1, period=-1) -> bool", py_btnp); 1133 + py_bind(mod, "cls(color=0)", py_cls); 1134 + py_bind(mod, "spr(id: int, x: int, y: int, colorkey=-1, scale=1, flip=0, rotate=0, w=1, h=1)", 1135 + py_spr); 1136 + py_bind(mod, "print(text, x=0, y=0, color=15, fixed=False, scale=1, alt=False)", py_print); 1137 + py_bind(mod, "circ(x: int, y: int, radius: int, color: int)", py_circ); 1138 + py_bind(mod, "circb(x: int, y: int, radius: int, color: int)", py_circb); 1139 + py_bind(mod, "clip(x: int, y: int, width: int, height: int)", py_clip); 1140 + py_bind(mod, "elli(x: int, y: int, a: int, b: int, color: int)", py_elli); 1141 + py_bind(mod, "ellib(x: int, y: int, a: int, b: int, color: int)", py_ellib); 1142 + py_bind(mod, "exit()", py_exit); 1143 + py_bind(mod, "fget(sprite_id: int, flag: int) -> bool", py_fget); 1144 + py_bind(mod, "fset(sprite_id: int, flag: int, b: bool)", py_fset); 1145 + py_bind(mod, "font(text: str, x: int, y: int, chromakey: int, char_width=8, char_height=8, fixed=False, scale=1, alt=False) -> int", py_font); 1146 + py_bind(mod, "key(code=-1) -> bool", py_key); 1147 + py_bind(mod, "keyp(code=-1, hold=-1, period=-17) -> int", py_keyp); 1148 + py_bind(mod, "line(x0: int, y0: int, x1: int, y1: int, color: int)", py_line); 1149 + py_bind(mod, "map(x=0, y=0, w=30, h=17, sx=0, sy=0, colorkey=-1, scale=1, remap=None)", py_map); 1150 + py_bind(mod, "memcpy(dest: int, source: int, size: int)", py_memcpy); 1151 + py_bind(mod, "memset(dest: int, value: int, size: int)", py_memset); 1152 + py_bind(mod, "mget(x: int, y: int) -> int", py_mget); 1153 + py_bind(mod, "mset(x: int, y: int, tile_id: int)", py_mset); 1154 + py_bind(mod, "mouse() -> tuple[int, int, bool, bool, bool, int, int]", py_mouse); 1155 + py_bind(mod, "music(track=-1, frame=-1, row=-1, loop=True, sustain=False, tempo=-1, speed=-1)", py_music); 1156 + py_bind(mod, "peek(addr: int, bits=8) -> int", pyy_peek); 1157 + py_bind(mod, "peek1(addr: int) -> int", py_peek1); 1158 + py_bind(mod, "peek2(addr: int) -> int", py_peek2); 1159 + py_bind(mod, "peek4(addr: int) -> int", py_peek4); 1160 + py_bind(mod, "pix(x: int, y: int, color: int=None) -> int | None", py_pix); 1161 + py_bind(mod, "pmem(index: int, value: int=None) -> int", py_pmem); 1162 + py_bind(mod, "poke(addr: int, value: int, bits=8)", py_poke); 1163 + py_bind(mod, "poke1(addr: int, value: int)", py_poke1); 1164 + py_bind(mod, "poke2(addr: int, value: int)", py_poke2); 1165 + py_bind(mod, "poke4(addr: int, value: int)", py_poke4); 1166 + py_bind(mod, "rect(x: int, y: int, w: int, h: int, color: int)", py_rect); 1167 + py_bind(mod, "rectb(x: int, y: int, w: int, h: int, color: int)", py_rectb); 1168 + py_bind(mod, "reset()", py_reset); 1169 + py_bind(mod, "sfx(id: int, note=-1, duration=-1, channel=0, volume=15, speed=0)", py_sfx); 1170 + py_bind(mod, "sync(mask=0, bank=0, tocart=False)", py_sync); 1171 + py_bind(mod, "ttri(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float, u1: float, v1: float, u2: float, v2: float, u3: float, v3: float, texsrc=0, chromakey=-1, z1=0.0, z2=0.0, z3=0.0)", py_ttri); 1172 + py_bind(mod, "time() -> int", py_time); 1173 + py_bind(mod, "trace(message, color=15)", py_trace); 1174 + py_bind(mod, "tri(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float, color: int)", py_tri); 1175 + py_bind(mod, "trib(x1: float, y1: float, x2: float, y2: float, x3: float, y3: float, color: int)", py_trib); 1176 + py_bind(mod, "tstamp() -> int", py_tstamp); 1177 + py_bind(mod, "vbank(bank: int=None) -> int", py_vbank); 1348 1178 return true; 1349 1179 } 1350 1180 1351 - void closePython(tic_mem* tic) 1181 + void close_pkpy_v2(tic_mem* tic) 1352 1182 { 1353 1183 tic_core* core = (tic_core*)tic; 1354 - 1355 1184 if (core->currentVM) 1356 1185 { 1357 - pkpy_delete_vm(core->currentVM); 1358 1186 core->currentVM = NULL; 1187 + py_resetvm(); 1188 + //py_initialize();//reset 1359 1189 } 1360 1190 } 1361 1191 1362 - static void report_error(tic_core* core, char* prefix) { 1363 - pkpy_vm* vm = core->currentVM; 1364 - core->data->error(core->data->data, prefix); 1365 - char* message; 1366 - if (!pkpy_clear_error(vm, &message)) 1367 - core->data->error(core->data->data, "error was thrown but not register (pocketpy c binding bug)"); 1368 - else 1369 - core->data->error(core->data->data, message); 1370 - } 1371 - 1372 - static bool initPython(tic_mem* tic, const char* code) 1192 + static bool init_pkpy_v2(tic_mem* tic, const char* code) 1373 1193 { 1374 - N._tic_core = pkpy_name("_tic_core"); 1375 - N.len = pkpy_name("len"); 1376 - N.__getitem__ = pkpy_name("__getitem__"); 1377 - N.TIC = pkpy_name("TIC"); 1378 - N.BOOT = pkpy_name("BOOT"); 1379 - N.SCN = pkpy_name("SCN"); 1380 - N.BDR = pkpy_name("BDR"); 1381 - N.MENU = pkpy_name("MENU"); 1382 - 1383 - closePython(tic); 1194 + //maybe some config here 1195 + py_initialize(); 1384 1196 tic_core* core = (tic_core*)tic; 1385 - 1386 - pkpy_vm* vm = pkpy_new_vm(false); 1387 - 1388 - core->currentVM = vm; 1197 + close_pkpy_v2(tic); 1198 + N.TIC = py_name("TIC"); 1199 + N.BOOT = py_name("BOOT"); 1200 + N.SCN = py_name("SCN"); 1201 + N.BDR = py_name("BDR"); 1202 + N.MENU = py_name("MENU"); 1389 1203 1390 - if (!setup_core(vm, core)) { 1391 - report_error(core, "problem binding tic_core (pocketpy c binding bug)\n"); 1392 - return false; 1393 - } 1394 - if (!setup_c_bindings(vm)) 1204 + setup_core(core); 1205 + core->currentVM = (void*)py_retval(); 1206 + if (!bind_pkpy_v2()) 1395 1207 { 1396 - report_error(core, "problem setting up the c bindings (pocketpy c binding bug\n"); 1397 - return false; 1208 + return RuntimeError("Binding func failed"); 1398 1209 } 1399 - 1400 - if(!pkpy_exec(vm, code)) 1210 + if (!py_exec(code, "main.py", EXEC_MODE, NULL)) 1401 1211 { 1402 - report_error(core, "error while processing the main code\n"); 1403 - 1404 - return false; 1212 + //throw error 1213 + return py_throw_error(core, NULL); 1405 1214 } 1406 - 1407 1215 return true; 1408 1216 } 1409 1217 1410 - void callPythonTick(tic_mem* tic) 1218 + void tick_pkpy_v2(tic_mem* tic) 1411 1219 { 1412 1220 tic_core* core = (tic_core*)tic; 1413 - if (!core->currentVM) 1414 - return; 1221 + if (!core->currentVM) return; //no vm 1415 1222 1416 - if(!pkpy_getglobal(core->currentVM, N.TIC)) return; 1417 - 1418 - pkpy_push_null(core->currentVM); 1419 - if(!pkpy_vectorcall(core->currentVM, 0)){ 1420 - report_error(core, "error while running TIC\n"); 1421 - }else{ 1422 - pkpy_pop_top(core->currentVM); 1223 + py_GlobalRef py_tick = py_getglobal(N.TIC); 1224 + if (!py_tick) return; 1225 + py_push(py_tick); 1226 + py_pushnil(); 1227 + if (!py_vectorcall(0, 0)) 1228 + { 1229 + py_throw_error(core, "TIC running error!"); 1423 1230 } 1424 1231 } 1425 - void callPythonBoot(tic_mem* tic) { 1426 - tic_core* core = (tic_core*)tic; 1427 - if (!core->currentVM) 1428 - return; 1429 1232 1430 - if(!pkpy_getglobal(core->currentVM, N.BOOT)) return; 1431 - 1432 - pkpy_push_null(core->currentVM); 1433 - if(!pkpy_vectorcall(core->currentVM, 0)){ 1434 - report_error(core, "error while running BOOT\n"); 1435 - }else{ 1436 - pkpy_pop_top(core->currentVM); 1437 - } 1438 - } 1439 - 1440 - void callPythonScanline(tic_mem* tic, s32 row, void* data) { 1233 + void boot_pkpy_v2(tic_mem* tic) 1234 + { 1441 1235 tic_core* core = (tic_core*)tic; 1442 - if (!core->currentVM) 1443 - return; 1444 - 1445 - if(!pkpy_getglobal(core->currentVM, N.SCN)) return; 1236 + if (!core->currentVM) return; //no vm 1446 1237 1447 - pkpy_push_null(core->currentVM); 1448 - pkpy_push_int(core->currentVM, row); 1449 - if(!pkpy_vectorcall(core->currentVM, 1)){ 1450 - report_error(core, "error while running SCN\n"); 1451 - }else{ 1452 - pkpy_pop_top(core->currentVM); 1238 + py_GlobalRef py_boot = py_getglobal(N.BOOT); 1239 + if (!py_boot) return; 1240 + py_push(py_boot); 1241 + py_pushnil(); 1242 + if (!py_vectorcall(0, 0)) 1243 + { 1244 + py_throw_error(core, "BOOT running error!"); 1453 1245 } 1454 1246 } 1455 1247 1456 - void callPythonBorder(tic_mem* tic, s32 row, void* data) { 1248 + void callback_scanline(tic_mem* tic, s32 row, void* data) 1249 + { 1457 1250 tic_core* core = (tic_core*)tic; 1458 - if (!core->currentVM) 1459 - return; 1460 - 1461 - if(!pkpy_getglobal(core->currentVM, N.BDR)) return; 1462 - 1463 - pkpy_push_null(core->currentVM); 1464 - pkpy_push_int(core->currentVM, row); 1465 - if(!pkpy_vectorcall(core->currentVM, 1)){ 1466 - report_error(core, "error while running BDR\n"); 1467 - }else{ 1468 - pkpy_pop_top(core->currentVM); 1469 - } 1470 - } 1471 - 1472 - void callPythonMenu(tic_mem* tic, s32 index, void* data) { 1473 - tic_core* core = (tic_core*)tic; 1474 - if (!core->currentVM) 1475 - return; 1476 - 1477 - if(pkpy_getglobal(core->currentVM, N.MENU)) return; 1251 + if (!core->currentVM) return; //no vm 1478 1252 1479 - pkpy_push_null(core->currentVM); 1480 - pkpy_push_int(core->currentVM, index); 1481 - if(!pkpy_vectorcall(core->currentVM, 1)){ 1482 - report_error(core, "error while running MENU\n"); 1483 - }else{ 1484 - pkpy_pop_top(core->currentVM); 1253 + py_GlobalRef py_scn = py_getglobal(N.SCN); 1254 + if (!py_scn) return; 1255 + py_push(py_scn); 1256 + py_pushnil(); 1257 + py_Ref py_row = py_retval(); 1258 + py_newint(py_row, row); 1259 + py_push(py_row); 1260 + if (!py_vectorcall(1, 0)) 1261 + { 1262 + py_throw_error(core, "SCANLINE running error!"); 1485 1263 } 1486 1264 } 1487 1265 1488 - static bool is_alnum_(char c) { 1489 - return ( 1490 - (c >= 'a' && c <= 'z') 1491 - || (c >= 'A' && c <= 'Z') 1492 - || (c >= '0' && c <= '9') 1493 - || (c == '_') 1494 - ); 1495 - } 1496 - 1497 - static const tic_outline_item* getPythonOutline(const char* code, s32* size) 1266 + void callback_border(tic_mem* tic, s32 row, void* data) 1498 1267 { 1499 - 1500 - *size = 0; 1501 - static tic_outline_item* items = NULL; 1502 - if (items) 1503 - { 1504 - free(items); 1505 - items = NULL; 1506 - } 1507 - 1508 - const char* name_start; 1509 - 1510 - start: 1511 - if (*code == 0) goto end; 1512 - else if (strncmp(code, "def", 3) == 0) { code += 3; goto next_word_ws; } 1513 - else if (*code == '#') { code++; goto comment; } 1514 - else if (strncmp(code, "\"\"\"", 3) == 0) { code += 3; goto multiline_string; } 1515 - else if (*code == '"') { code++; goto string; } 1516 - else { code++; goto start; } 1517 - 1518 - next_word_ws : 1519 - if (*code == 0) goto end; 1520 - else if (*code=='\t' || *code==' ' || *code=='\n') { code++; goto next_word_ws; } 1521 - else if (is_alnum_(*code)) { name_start = code; code++; goto next_word; } 1522 - else goto start; 1268 + tic_core* core = (tic_core*)tic; 1269 + if (!core->currentVM) return; //no vm 1523 1270 1524 - next_word : 1525 - if (is_alnum_(*code)) { code++; goto next_word; } 1526 - else //store item 1271 + py_GlobalRef py_bdr = py_getglobal(N.BDR); 1272 + if (!py_bdr) return; 1273 + py_push(py_bdr); 1274 + py_pushnil(); 1275 + py_Ref py_row = py_retval(); 1276 + py_newint(py_row, row); 1277 + py_push(py_row); 1278 + if (!py_vectorcall(1, 0)) 1527 1279 { 1528 - items = realloc(items, (*size + 1) * sizeof(tic_outline_item)); 1529 - items[*size].pos = name_start; 1530 - items[*size].size = (s32)(code - name_start); 1531 - (*size)++; 1532 - goto start; 1280 + py_throw_error(core, "BORDER running error!"); 1533 1281 } 1534 - 1535 - comment : 1536 - if (*code == 0) goto end; 1537 - else if (*code == '\n') { code++; goto start; } 1538 - else { code++; goto comment; } 1539 - 1540 - string : 1541 - if (*code == 0) goto end; 1542 - else if (*code == '"') { code++; goto start; } 1543 - else if (*code == '\\') { code++; goto string_escape; } 1544 - else { code++; goto string; } 1545 - 1546 - string_escape : 1547 - if (*code == 0) goto end; 1548 - else { code++; goto string; } 1549 - 1550 - multiline_string : 1551 - if (*code == 0) goto end; 1552 - else if (*code == '\\') { code++; goto multiline_string_escape; } 1553 - else if (strncmp(code, "\"\"\"", 3) == 0) { code += 3; goto start; } 1554 - else {code++; goto string; } 1555 - 1556 - multiline_string_escape : 1557 - if (*code == 0) goto end; 1558 - else { code++; goto multiline_string; } 1559 - 1560 - end: 1561 - return items; 1562 - 1563 1282 } 1564 - 1565 - void evalPython(tic_mem* tic, const char* code) 1283 + void callback_menu(tic_mem* tic, s32 index, void* data) 1566 1284 { 1567 1285 tic_core* core = (tic_core*)tic; 1568 - pkpy_vm* vm = core->currentVM; 1569 - if (!vm) return; 1286 + if (!core->currentVM) return; //no vm 1570 1287 1571 - if(!pkpy_exec(vm, code)) 1288 + py_GlobalRef py_menu = py_getglobal(N.MENU); 1289 + if (!py_menu) return; 1290 + py_push(py_menu); 1291 + py_pushnil(); 1292 + py_Ref py_idx = py_retval(); 1293 + py_newint(py_idx, index); 1294 + py_push(py_idx); 1295 + if (!py_vectorcall(1, 0)) 1572 1296 { 1573 - report_error(core, "error while evaluating the code\n"); 1297 + py_throw_error(core, "MENU running error!"); 1574 1298 } 1575 1299 } 1576 1300 1577 - 1578 1301 static const char* const PythonKeywords[] = 1579 - { 1580 - "is not", "not in", "yield from", 1581 - "class", "import", "as", "def", "lambda", "pass", "del", "from", "with", "yield", 1582 - "None", "in", "is", "and", "or", "not", "True", "False", "global", "try", "except", "finally", 1583 - "while", "for", "if", "elif", "else", "break", "continue", "return", "assert", "raise" 1584 - }; 1302 + { 1303 + "is not", "not in", "yield from", 1304 + "class", "import", "as", "def", "lambda", "pass", "del", "from", "with", "yield", 1305 + "None", "in", "is", "and", "or", "not", "True", "False", "global", "try", "except", "finally", 1306 + "while", "for", "if", "elif", "else", "break", "continue", "return", "assert", "raise"}; 1585 1307 1586 1308 static const u8 DemoRom[] = 1587 - { 1588 - #include "../build/assets/pythondemo.tic.dat" 1309 + { 1310 + #include "../build/assets/pythondemo.tic.dat" 1589 1311 }; 1590 1312 1591 1313 static const u8 MarkRom[] = 1592 - { 1593 - #include "../build/assets/pythonmark.tic.dat" 1314 + { 1315 + #include "../build/assets/pythonmark.tic.dat" 1594 1316 }; 1595 1317 1596 1318 TIC_EXPORT const tic_script EXPORT_SCRIPT(Python) = 1597 - { 1598 - .id = 20, 1599 - .name = "python", 1600 - .fileExtension = ".py", 1601 - .projectComment = "#", 1602 - .init = initPython, 1603 - .close = closePython, 1604 - .tick = callPythonTick, 1605 - .boot = callPythonBoot, 1606 - 1607 - .callback = 1608 1319 { 1609 - .scanline = callPythonScanline, 1610 - .border = callPythonBorder, 1611 - .menu = callPythonMenu, 1612 - }, 1320 + .id = 20, 1321 + .name = "python", 1322 + .fileExtension = ".py", 1323 + .projectComment = "#", 1324 + .init = init_pkpy_v2, 1325 + .close = close_pkpy_v2, 1326 + .tick = tick_pkpy_v2, 1327 + .boot = boot_pkpy_v2, 1613 1328 1614 - .getOutline = getPythonOutline, 1615 - .eval = evalPython, 1329 + .callback = 1330 + { 1331 + .scanline = callback_scanline, 1332 + .border = callback_border, 1333 + .menu = callback_menu, 1334 + }, 1616 1335 1617 - .blockCommentStart = NULL, 1618 - .blockCommentEnd = NULL, 1619 - .blockCommentStart2 = NULL, 1620 - .blockCommentEnd2 = NULL, 1621 - .singleComment = "#", 1622 - .blockStringStart = NULL, 1623 - .blockStringEnd = NULL, 1624 - .blockEnd = NULL, 1625 - .stdStringStartEnd = "'\"", 1336 + .getOutline = NULL, 1337 + .eval = NULL, 1338 + //above is a must need 1339 + .blockCommentStart = NULL, 1340 + .blockCommentEnd = NULL, 1341 + .blockCommentStart2 = NULL, 1342 + .blockCommentEnd2 = NULL, 1343 + .singleComment = "#", 1344 + .blockStringStart = NULL, 1345 + .blockStringEnd = NULL, 1346 + .blockEnd = NULL, 1347 + .stdStringStartEnd = "'\"", 1626 1348 1627 - .keywords = PythonKeywords, 1628 - .keywordsCount = COUNT_OF(PythonKeywords), 1349 + .keywords = PythonKeywords, 1350 + .keywordsCount = COUNT_OF(PythonKeywords), 1629 1351 1630 - .demo = {DemoRom, sizeof DemoRom}, 1631 - .mark = {MarkRom, sizeof MarkRom, "pythonmark.tic"}, 1352 + .demo = {DemoRom, sizeof DemoRom}, 1353 + .mark = {MarkRom, sizeof MarkRom, "pythonmark.tic"}, 1632 1354 };