Vkeyboard typing mode
[jpcrr.git] / luakernel.lua
blob056ea4ac9fecf681e685b2bc3df9e20b927d4422
1 --
2 -- Copyright 2009 Ilari Liusvaara
3 --
4 -- Licenced under GNU GPL v2.
5 --
7 -- Exported tables:
8 -- - args
9 -- Contains user-level arguments.
11 -- Exported functions:
12 -- - All lua standard functions in tables main, "coroutine", "string" and "table".
13 -- - modulus_split(number num, number...)
14 -- Returns quotents and quotent remainders of number divided by given dividers.
15 -- The number of results returned is equal to number of numbers passed. The
16 -- dividers must be in decreasing order and all dividers must be nonzero and
17 -- should be >1.
18 -- - assert(object ret, object err)
19 -- If ret is considered true, return ret. Otherwise raise err as error. Exception
20 -- if err is nil too, then ret is returned anyway.
21 -- - bit.none(number...)
22 -- Returns 48-bit number that has those bits set that are set in none of its
23 -- arguments.
24 -- - bit.any(number...)
25 -- Returns 48-bit number that has those bits set that are set in any of its
26 -- arguments.
27 -- - bit.parity(number...)
28 -- Returns 48-bit number that has those bits set that are set in even number
29 -- of its arguments.
30 -- - bit.any(number...)
31 -- Returns 48-bit number that has those bits set that are set in all of its
32 -- arguments.
33 -- - bit.lshift(number num, number shift)
34 -- Returns 48 rightmost bits of num shifted by shift places left. Going outside
35 -- shift range 0-47 produces unpredicatable results.
36 -- - bit.rshift(number num, number shift)
37 -- Returns 48 rightmost bits of num shifted by shift places right. Going outside
38 -- shift range 0-47 produces unpredicatable results.
39 -- - bit.arshift(number num, number shift)
40 -- Returns 48 rightmost bits of num shifted by shift places right and bit 47
41 -- value copied to all incoming bits. Going outside shift range 0-47 produces
42 -- unpredicatable results.
43 -- - bit.add(number...)
44 -- Returns sum of all passed numbers modulo 2^48.
45 -- - bit.addneg(number...)
46 -- Returns 0 minus sum of all passed numbers modulo 2^48.
47 -- - bit.addalt(number...)
48 -- Returns sum of odd arguments (first is odd) minus sum of even arguments modulo
49 -- 2^48.
50 -- - bit.tohex(number num)
51 -- Returns hexadecimal string representation of number.
52 -- - jpcrr.wait_message()
53 -- Waits for message (string) to come from message queue. Reutrns nil if interrupted.
54 -- - jpcrr.poll_message()
55 -- Checks if there is message from message queue. Reutrns message if there is one,
56 -- nil if none.
57 -- - jpcrr.wait_pc_stop()
58 -- Waits for PC execution to stop (but does not attempt to actually stop it).
59 -- If script is in frame hold, the frame hold is released.
60 -- Returns true if execution got stopped, false if interrupted.
61 -- - jpcrr.pc_running()
62 -- Returns true if PC is running.
63 -- - jpcrr.clock_time()
64 -- Returns current time or nil if no PC.
65 -- - jpcrr.pc_connected()
66 -- Returns true if PC is connected.
67 -- - jpcrr.wait_pc_attach()
68 -- Wait for PC to attach.
69 -- - jpcrr.next_frame()
70 -- Wait for next frame output hold.
71 -- - jpcrr.in_frame_hold()
72 -- Returns true if in frame hold, false otherwise.
73 -- - jpcrr.keypressed(number key)
74 -- Return true if key is pressed, else false.
75 -- - jpcrr.wait_vga()
76 -- Waits for VGA to enter frame hold mode. Frame hold happens once per frame.
77 -- - jpcrr.release_vga()
78 -- Allow VGA to exit frame hold mode. Wait for frame hold first.
79 -- - jpcrr.vga_resolution()
80 -- Return VGA x and y resolutions. -1x-1 or 0x0 is returned if no valid resolution.
81 -- Should only be called during frame hold.
82 -- - jpcrr.hud.left_gap(number flags, number gap)
83 -- Set left gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
84 -- 1 (2) is set, dump to video dump.
85 -- - jpcrr.hud.right_gap(number flags, number gap)
86 -- Set right gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
87 -- 1 (2) is set, dump to video dump.
88 -- - jpcrr.hud.top_gap(number flags, number gap)
89 -- Set top gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
90 -- 1 (2) is set, dump to video dump.
91 -- - jpcrr.hud.bottom_gap(number flags, number gap)
92 -- Set bottom gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
93 -- 1 (2) is set, dump to video dump.
94 -- - jpcrr.hud.white_solid_box(number flags, number x, number y, number w, number h)
95 -- Draw with solid opaque box.
96 -- - jpcrr.hud.box(number flags, number x, number y, number w, number h, number linethick,
97 -- number lineRed, number lineGreen, number lineBlue, number lineAlpha,
98 -- number fillRed, number fillGreen, number fillBlue, number fillAlpha)
99 -- Draw box with specified size, border line thickness, line color and fill color.
100 -- - jpcrr.hud.circle(number flags, number x, number y, number r, number linethick,
101 -- number lineRed, number lineGreen, number lineBlue, number lineAlpha,
102 -- number fillRed, number fillGreen, number fillBlue, number fillAlpha)
103 -- Draw circle with specified size, border line thickness, line color and fill color.
104 -- - jpcrr.hud.bitmap(number flags, number x, number y, string bmap,
105 -- number fgRed, number fgGreen, number fgBlue, number fgAlpha,
106 -- number bgRed, number bgGreen, number bgBlue, number bgAlpha)
107 -- Draw bitmap with specified foreground color and background color.
108 -- - jpcrr.hud.bitmap_binary(number flags, number x, number y, string bmap,
109 -- number fgRed, number fgGreen, number fgBlue, number fgAlpha,
110 -- number bgRed, number bgGreen, number bgBlue, number bgAlpha)
111 -- Draw binary bitmap with specified foreground color and background color.
112 -- - jpcrr.joystick_state()
113 -- Returns nil if no joystick. Otherwise returns hold times for all four axis (numeric)
114 -- followed by button states (boolean).
115 -- - jpcrr.keyboard_leds()
116 -- Returns nil if no keyboard, false if LED status is unknown. Otherwise returns three
117 -- booleans, first being state of num lock, second being state of caps lock and third
118 -- being state of scroll lock.
119 -- - jpcrr.component_encode(table components)
120 -- Return component encoding for specified components.
121 -- - jpcrr.component_decode(string line)
122 -- Return component decoding for specified line, nil/nil if it doesn't encode
123 -- anything, nil/string if parse error occurs (the error is the second return).
124 -- - jpcrr.save_state(string name)
125 -- Savestate into specified file. Returns name used.
126 -- - jpcrr.save_movie(string name)
127 -- Save movie into specified file. Returns name used.
128 -- - jpcrr.load_state_normal(string name)
129 -- Load specified savestate. Returns name used.
130 -- - jpcrr.load_state_preserve_events(string name)
131 -- Load specified savestate, preserving events. Returns name used.
132 -- - jpcrr.load_state_movie(string name)
133 -- Load specified savestate as movie. Returns name used.
134 -- - jpcrr.assemble()
135 -- Open system settings dialog.
136 -- - jpcrr.change_authors()
137 -- Open change authors dialog.
138 -- - jpcrr.exit = function()
139 -- Exit the Lua VM.
140 -- - jpcrr.ram_dump(string name, boolean binary)
141 -- Dump PC memory to specified file. If binary is true, dump is binary, otherwise
142 -- textual hexadecimal dump.
143 -- - jpcrr.write_byte(number addr, number value)
144 -- Write byte to specified physical address.
145 -- - jpcrr.write_word(number addr, number value)
146 -- Write word to specified physical address (little endian).
147 -- - jpcrr.write_dword(number addr, number value)
148 -- Write dword to specified physical address (little endian).
149 -- - jpcrr.read_byte(number addr)
150 -- Return byte from specified physical address.
151 -- - jpcrr.read_word(number addr)
152 -- Return word from specified physical address (little endian).
153 -- - jpcrr.read_dword(number addr)
154 -- Return dword from specified physical address (little endian).
155 -- - jpcrr.timed_trap(number nsecs)
156 -- Set trap after specified number of nanoseconds. Use nil as nsecs to disable.
157 -- - jpcrr.vretrace_start_trap(boolean is_on)
158 -- Set trap on vretrace start on/off.
159 -- - jpcrr.vretrace_end_trap(boolean is_on)
160 -- Set trap on vretrace end on/off.
161 -- - jpcrr.pc_start()
162 -- Start PC execution.
163 -- - jpcrr.pc_stop()
164 -- Stop PC execution.
165 -- - jpcrr.set_pccontrol_pos(number x, number y)
166 -- Set position of PCControl window.
167 -- - jpcrr.set_luaplugin_pos(number x, number y)
168 -- Set position of LuaPlugin window.
169 -- - jpcrr.set_pcmonitor_pos(number x, number y)
170 -- Set position of PCMonitor window.
171 -- - jpcrr.set_pcstartstoptest_pos(number x, number y)
172 -- Set position of PCStartStopTest window.
173 -- - jpcrr.set_virtualkeyboard_pos(number x, number y)
174 -- Set position of VirtualKeyboard window.
175 -- - jpcrr.stringlessthan(String x, String y)
176 -- Return true if x is before y in codepoint lexical order, otherwise false.
177 -- - jpcrr.screenshot(boolean include_hud)
178 -- Take screen shot (Requires monitor). If include_hud is true, include HUD
179 -- (as shown on screen). Note that this should only be called during frame
180 -- hold or results are pretty much undefined.
182 -- I/O functions have the following conventions. If function returns any real data, the first
183 -- return value returns this data or is nil. Otherwise first return value is true or false.
184 -- If first return value is nil or false, then the second return value gives textual error
185 -- message for failed operation, or is nil if EOF occured before anything was read.
187 -- Unlink, rename and mkdir don't follow this pattern. They just return true/false to signal
188 -- success or failure.
190 -- Specifying nil as name of file results random filename being used (it even works with unlink,
191 -- mkdir, rename and read-only access, but doesn't make any sense there).
193 -- Class: BinaryFile:
194 -- Binary file for RO or RW access. Methods are as follows:
195 -- - name()
196 -- Return name of file.
197 -- - length()
198 -- Return length of file.
199 -- - set_length(number length)
200 -- Truncate file to specified length
201 -- - read(number offset, number length)
202 -- Read up to length bytes from offset.
203 -- - write(number offset, string content)
204 -- Write content to specified offset.
205 -- - close()
206 -- Close the file.
207 -- Class: BinaryInput:
208 -- Binary file for sequential input. Methods are as follows:
209 -- - name()
210 -- Return name of file.
211 -- - four_to_five()
212 -- Return BinaryInput that is four to five decoding of this stream.
213 -- - text()
214 -- Return stream as TextInput.
215 -- - inflate()
216 -- Return BinaryInput that is inflate of this stream.
217 -- - read(number bytes)
218 -- Read up to bytes bytes from file.
219 -- - read()
220 -- Read the entiere file at once.
221 -- - close()
222 -- Close the file.
223 -- Character set for binary files is Latin-1.
225 -- Class: BinaryOutput:
226 -- Binary file for sequential output. Methods are as follows:
227 -- - name()
228 -- Return name of file.
229 -- - four_to_five()
230 -- Return BinaryOutput that writes four to five encoded output to this stream.
231 -- - text()
232 -- Return stream as TextOutput.
233 -- - deflate()
234 -- Return BinaryOutput that writes deflate output to this stream.
235 -- - write(string content)
236 -- Write string to file.
237 -- - close()
238 -- Close the file.
239 -- Character set for binary files is Latin-1.
241 -- Class: TextInput:
242 -- - name()
243 -- Return name of file.
244 -- - read()
245 -- Read line from file.
246 -- - read_component()
247 -- Read next componented line into array.
248 -- - lines()
249 -- Line iterator function.
250 -- - close()
251 -- Close the file.
252 -- Character set for text files is UTF-8.
254 -- Class: TextOutput:
255 -- - name()
256 -- Return name of file.
257 -- - write(string line)
258 -- Write line line to file.
259 -- - write_component(table components)
260 -- Write componented line.
261 -- - close()
262 -- Close the file.
263 -- Character set for text files is UTF-8.
265 -- Class ArchiveIn:
266 -- - member(string name)
267 -- Open substream for member name. The methods are the same as for io.opentextin.
268 -- - member_binary(string name)
269 -- Open binary (four to five) substream for member name. The methods are the same as
270 -- for io.openbinaryin.
271 -- - close()
272 -- Close the file. Any opened substreams are invalidated.
273 -- - member_list()
274 -- Return table listing all members of archive (sorted by name).
275 -- Class ArchiveOut:
276 -- - member(string name)
277 -- Open substream for member name. The methods are the same as for io.opentextout. Note that
278 -- previous member must be closed before next can open.
279 -- - member_binary(string name)
280 -- Open binary (four to five) substream for member name. The methods are the same as
281 -- for io.openbinaryout. Note that previous substream must be closed before next can open.
282 -- - commit()
283 -- Commit the file. No substream may be open. Closes the file.
284 -- - rollback()
285 -- Rollback the file. No substream may be open. Closes the file.
287 -- - io.open(string name, string mode) -> BinaryFile
288 -- Open file named @name in specified mode. The mode can be 'r' (read only) or 'rw' (read and
289 -- write).
290 -- - io.open_read(string name) -> BinaryInput
291 -- Open file named @name as binary input stream.
292 -- - io.open_write(string name) -> BinaryOutput
293 -- Open file named @name as binary input stream.
294 -- - io.open_arch_read(string name) -> ArchiveIn
295 -- Open file named @name as input archive.
296 -- - io.open_arch_write(string name) -> ArchiveOut
297 -- Open file named @name as output archive.
298 -- - io.mkdir(string name)
299 -- Create directory name. Returns name created on success, nil on failure.
300 -- - io.unlink(string name)
301 -- Delete file/directory name. Returns name deleted on success, nil on failure.
302 -- - io.rename(string old, string new)
303 -- Rename file old -> new. Returns old, new on success, nil on failure.
304 -- - io.transform.text()
305 -- Returns function that calls text() method of its parameter.
306 -- - io.transform.four_to_five()
307 -- Returns function that calls four_to_five() method of its parameter.
308 -- - io.transform.deflate()
309 -- Returns function that calls deflate() method of its parameter.
310 -- - io.transform.inflate()
311 -- Returns function that calls inflate() method of its parameter.
312 -- - io.dotransform(object obj, function...)
313 -- Call specified functions on specified object. If any function fails (first argument nil
314 -- or false), call close method on preceeding object. Otherwise call specified functions
315 -- chained left to right and return the result.
316 -- - io.dotransform2(object obj, string err, function...)
317 -- Similar to dotransform except if obj is nil or false, returns obj, err.
323 local handle, err, chunk, indication, k, v;
325 local loadmod = loadmodule;
326 loadmodule = nil;
328 local export_module_in = function(tab, modname, prefix)
329 local fun = loadmod(modname);
330 for k, v in pairs(fun) do
331 tab[(prefix or "") .. k] = v;
335 jpcrr = {};
336 jpcrr.hud = {};
337 bit = {};
338 io = {};
340 export_module_in(jpcrr, "org.jpc.luaextensions.Base");
341 export_module_in(jpcrr, "org.jpc.luaextensions.InputDevices");
342 export_module_in(jpcrr, "org.jpc.luaextensions.ComponentCoding", "component_");
343 export_module_in(bit, "org.jpc.luaextensions.Bitops");
345 -- Few misc functions.
346 assert = function(val, err)
347 if (not val) and err then
348 error(err);
350 return val;
353 modulus_split = function(number, ...)
354 local dividers = {...};
355 local results = {};
356 local rem;
358 for k, v in ipairs(dividers) do
359 rem = number % v;
360 table.insert(results, (number - rem) / v);
361 number = rem;
364 table.insert(results, number);
365 return unpack(results);
368 local getmtable = getmetatable;
369 local toString = tostring;
370 local inject_binary_file;
371 local inject_binary_input;
372 local inject_binary_output;
373 local inject_text_input;
374 local inject_text_output;
375 local inject_archive_input;
376 local inject_archive_output;
378 -- Class member injectors.
379 inject_binary_file = function(obj, name)
380 local _name = name;
381 getmtable(obj).name = function(obj)
382 return _name;
384 getmtable(obj).__index = function(tab, name)
385 local x = getmtable(obj)[name];
386 if x then
387 return x;
389 error("Invalid method " .. name .. " for BinaryFile");
391 return obj;
394 inject_binary_input = function(obj, underlying, name)
395 local _name = name;
396 local old_four_to_five = getmtable(obj).four_to_five;
397 local old_inflate = getmtable(obj).inflate;
398 local old_text = getmtable(obj).text;
399 local old_read = getmtable(obj).read;
400 local old_close = getmtable(obj).close;
401 local underlying_object = underlying;
403 getmtable(obj).name = function(obj)
404 return _name;
406 getmtable(obj).four_to_five = function(obj)
407 local res, err;
408 res, err = old_four_to_five(obj);
409 if not res then
410 return res, err;
412 return inject_binary_input(res, obj, "four-to-five<" .. _name .. ">");
414 getmtable(obj).inflate = function(obj)
415 local res, err;
416 res, err = old_inflate(obj);
417 if not res then
418 return res, err;
420 return inject_binary_input(res, obj, "inflate<" .. _name .. ">");
422 getmtable(obj).text = function(obj)
423 local res, err;
424 res, err = old_text(obj);
425 if not res then
426 return res, err;
428 return inject_text_input(res, obj, "text<" .. _name .. ">");
430 getmtable(obj).read = function(obj, toread)
431 if toread then
432 return old_read(obj, toread);
433 else
434 local res = "";
435 local ret, err;
436 while true do
437 ret, err = old_read(obj, 16384);
438 if not ret then
439 if not err then
440 return res;
442 return nil, err;
444 res = res .. ret;
448 getmtable(obj).close = function(obj)
449 local ret, err, ret2, err2;
450 ret, err = old_close(obj);
451 if underlying_object then ret2, err2 = underlying_object:close(); end
452 if ret and not ret2 then
453 err = err2;
454 ret = ret2;
456 return ret, err;
458 getmtable(obj).__index = function(tab, name)
459 local x = getmtable(obj)[name];
460 if x then
461 return x;
463 error("Invalid method " .. name .. " for BinaryInput");
465 return obj;
468 inject_binary_output = function(obj, underlying, name)
469 local _name = name;
470 local old_four_to_five = getmtable(obj).four_to_five;
471 local old_deflate = getmtable(obj).deflate;
472 local old_text = getmtable(obj).text;
473 local old_close = getmtable(obj).close;
474 local underlying_object = underlying;
476 getmtable(obj).name = function(obj)
477 return _name;
479 getmtable(obj).four_to_five = function(obj)
480 local res, err;
481 res, err = old_four_to_five(obj);
482 if not res then
483 return res, err;
485 return inject_binary_output(res, obj, "four-to-five<" .. _name .. ">");
487 getmtable(obj).deflate = function(obj)
488 local res, err;
489 res, err = old_deflate(obj);
490 if not res then
491 return res, err;
493 return inject_binary_output(res, obj, "deflate<" .. _name .. ">");
495 getmtable(obj).text = function(obj)
496 local res, err;
497 res, err = old_text(obj);
498 if not res then
499 return res, err;
501 return inject_text_output(res, obj, "text<" .. _name .. ">");
503 getmtable(obj).close = function(obj)
504 local ret, err, ret2, err2;
505 ret, err = old_close(obj);
506 if underlying_object then ret2, err2 = underlying_object:close(); end
507 if ret and not ret2 then
508 err = err2;
509 ret = ret2;
511 return ret, err;
513 getmtable(obj).__index = function(tab, name)
514 local x = getmtable(obj)[name];
515 if x then
516 return x;
518 error("Invalid method " .. name .. " for BinaryOutput");
520 return obj;
523 inject_text_input = function(obj, underlying, name)
524 local _name = name;
525 local old_close = getmtable(obj).close;
526 local underlying_object = underlying;
528 getmtable(obj).lines = function(obj)
529 return function(state, prevline)
530 return state:read();
531 end, obj, nil;
533 getmtable(obj).name = function(obj)
534 return _name;
536 getmtable(obj).close = function(obj)
537 local ret, err, ret2, err2;
538 ret, err = old_close(obj);
539 if underlying_object then ret2, err2 = underlying_object:close(); end
540 if ret and not ret2 then
541 err = err2;
542 ret = ret2;
544 return ret, err;
546 getmtable(obj).__index = function(tab, name)
547 local x = getmtable(obj)[name];
548 if x then
549 return x;
551 error("Invalid method " .. name .. " for TextInput");
553 return obj;
556 inject_text_output = function(obj, underlying, name)
557 local _name = name;
558 local old_close = getmtable(obj).close;
559 local underlying_object = underlying;
561 getmtable(obj).name = function(obj)
562 return _name;
564 getmtable(obj).close = function(obj)
565 local ret, err, ret2, err2;
566 ret, err = old_close(obj);
567 if underlying_object then ret2, err2 = underlying_object:close(); end
568 if ret and underlying_object then
569 err = err2;
570 ret = ret2;
572 return ret, err;
574 getmtable(obj).__index = function(tab, name)
575 local x = getmtable(obj)[name];
576 if x then
577 return x;
579 error("Invalid method " .. name .. " for TextOutput");
581 return obj;
584 inject_archive_input = function(obj, name)
585 local _name = name;
586 local old_member = getmtable(obj).member;
587 local old_member_list = getmtable(obj).member_list;
588 getmtable(obj).member = function(obj, member)
589 local res, err;
590 res, err = old_member(obj, member);
591 if not res then
592 return res, err;
594 return inject_binary_input(res, nil, _name .. "[" .. member .. "]");
596 getmtable(obj).name = function(obj)
597 return _name;
599 getmtable(obj).member_list = function(obj)
600 local tab = old_member_list(obj);
601 if tab then table.sort(tab, jpcrr.stringlessthan); end
602 return tab;
604 getmtable(obj).__index = function(tab, name)
605 local x = getmtable(obj)[name];
606 if x then
607 return x;
609 error("Invalid method " .. name .. " for ArchiveInput");
611 return obj;
614 inject_archive_output = function(obj, name)
615 local _name = name;
616 local old_member = getmtable(obj).member;
617 getmtable(obj).member = function(obj, member)
618 local res, err;
619 res, err = old_member(obj, member);
620 if not res then
621 return res, err;
623 return inject_binary_output(res, nil, _name .. "[" .. member .. "]");
625 getmtable(obj).name = function(obj)
626 return _name;
628 getmtable(obj).__index = function(tab, name)
629 local x = getmtable(obj)[name];
630 if x then
631 return x;
633 error("Invalid method " .. name .. " for ArchiveOutput");
635 return obj;
639 -- Redefined print.
641 local rprint = print_console_msg;
642 print_console_msg = nil;
643 print = function(...)
644 local x = "";
645 local y = {...};
646 local i;
647 for i = 1,#y do
648 if i > 1 then
649 x = x .. "\t" .. toString(y[i]);
650 else
651 x = toString(y[i]);
654 rprint(x);
656 print_console_msg = nil;
659 -- I/O routines.
660 local stringfind = string.find;
661 local randname = loadmod("org.jpc.luaextensions.DelayedDelete").random_temp_name;
662 local path = args["luapath"] or ".";
663 local toresourcename = function(resname)
664 if not resname then
665 return randname(path .. "/", "luatemp-");
668 if not stringfind(resname, "[%d%l%u_%-]") then
669 error("Bad resource name (case 1): " .. resname);
671 if stringfind(resname, "^/") then
672 error("Bad resource name (case 2): " .. resname);
674 if stringfind(resname, "%.%.") then
675 error("Bad resource name (case 3): " .. resname);
677 if stringfind(resname, "\\") then
678 error("Bad resource name (case 4): " .. resname);
681 return resname, path .. "/" .. resname;
686 local openbinin = loadmod("org.jpc.luaextensions.BinaryInFile").open;
687 local openbinout = loadmod("org.jpc.luaextensions.BinaryOutFile").open;
688 local openarchin = loadmod("org.jpc.luaextensions.ArchiveIn").open;
689 local openarchout = loadmod("org.jpc.luaextensions.ArchiveOut").open;
690 local openbinary = loadmod("org.jpc.luaextensions.BinaryFile").open;
692 local baseFS = loadmod("org.jpc.luaextensions.BaseFSOps");
693 local mkdir = baseFS.mkdir;
694 local unlink = baseFS.unlink;
695 local rename = baseFS.rename;
697 local getmtable = getmetatable;
699 loadfile = function(_script)
700 local file, file2, err, content;
701 local x, y;
702 x, y = toresourcename(_script);
703 file, err = openbinin(y, "r");
704 if not file then
705 return nil, "Can't open " .. _script .. ": " .. err;
707 file2, err = file:text();
708 if not file2 then
709 return nil, "Can't transform " .. _script .. ": " .. err;
711 content = "";
712 line = file2:read();
713 while line do
714 content = content .. line .. "\n";
715 line = file2:read();
717 file2:close();
718 file:close();
719 return loadstring(content, _script);
722 io.open = function(name, mode)
723 local _name;
724 local res, err;
725 local y;
726 _name, y = toresourcename(name);
727 res, err = openbinary(y, mode);
728 if not res then
729 return res, err;
731 return inject_binary_file(res, _name);
734 io.open_arch_read = function(name)
735 local _name = name;
736 local res, err;
737 local y;
738 _name, y = toresourcename(name);
739 res, err = openarchin(y);
740 if not res then
741 return res, err;
743 return inject_archive_input(res, _name);
746 io.open_arch_write = function(name)
747 local _name = name;
748 local res, err;
749 local y;
750 _name, y = toresourcename(name);
751 res, err = openarchout(y);
752 if not res then
753 return res, err;
755 return inject_archive_output(res, _name);
758 io.open_read = function(name)
759 local _name = name;
760 local res, err;
761 local y;
762 _name, y = toresourcename(name);
763 res, err = openbinin(y);
764 if not res then
765 return res, err;
767 return inject_binary_input(res, nil, _name);
770 io.open_write = function(name)
771 local _name = name;
772 local res, err;
773 local y;
774 _name, y = toresourcename(name);
775 res, err = openbinout(y);
776 if not res then
777 return res, err;
779 return inject_binary_output(res, nil, _name);
782 io.mkdir = function(name)
783 local _name, y;
784 _name, y = toresourcename(name);
785 if mkdir(y) then
786 return _name;
787 else
788 return nil;
792 io.unlink = function(name)
793 local _name, y;
794 _name, y = toresourcename(name);
795 if unlink(y) then
796 return _name;
797 else
798 return nil;
802 io.rename = function(name1, name2)
803 local _name, y;
804 local _name2, y2;
805 _name, y = toresourcename(name1);
806 _name2, y2 = toresourcename(name2);
807 if rename(y, y2) then
808 return _name, _name2;
809 else
810 return nil;
814 io.transform = {};
816 io.transform.text = function()
817 return function(obj)
818 return obj:text();
822 io.transform.four_to_five = function()
823 return function(obj)
824 return obj:four_to_five();
828 io.transform.inflate = function()
829 return function(obj)
830 return obj:inflate();
834 io.transform.deflate = function()
835 return function(obj)
836 return obj:deflate();
840 io.dotransform = function(obj, ...)
841 local todo = {...};
842 local k, v;
843 local obj2, err;
844 for k, v in ipairs(todo) do
845 obj2, err = v(obj);
846 if not obj2 then
847 obj:close();
848 return nil, err;
850 obj = obj2;
852 return obj;
855 io.dotransform2 = function(obj, err, ...)
856 if not obj then
857 return obj, err;
859 return io.dotransform(obj, err, ...);
864 jpcrr.next_frame = function(name)
865 while true do
866 if not jpcrr.pc_connected() then
867 jpcrr.wait_pc_attach();
869 if jpcrr.in_frame_hold() then
870 jpcrr.release_vga();
872 if jpcrr.wait_vga() then
873 return;
879 -- Various stuff built on top of ECI.
880 local invoke = jpcrr.invoke;
881 local invokecall = jpcrr.call;
882 local invokesync = jpcrr.invoke_synchronous;
884 jpcrr.save_state = function(name)
885 local _name, _fname;
886 _name, _fname = toresourcename(name);
887 invokesync("state-save", {_fname});
888 return _name;
891 jpcrr.save_movie = function(name)
892 local _name, _fname;
893 _name, _fname = toresourcename(name);
894 invokesync("movie-save", {_name});
895 return _name;
898 jpcrr.load_state_normal = function(name)
899 local _name, _fname;
900 _name, _fname = toresourcename(name);
901 invokesync("state-load", {_name});
902 return _name;
905 jpcrr.load_state_preserve_events = function(name)
906 local _name, _fname;
907 _name, _fname = toresourcename(name);
908 invokesync("state-load-noevents", {_name});
909 return _name;
912 jpcrr.load_state_movie = function(name)
913 local _name, _fname;
914 _name, _fname = toresourcename(name);
915 invokesync("state-load-movie", {_name});
916 return _name;
919 jpcrr.assemble = function()
920 invokesync("pc-assemble");
923 jpcrr.change_authors = function()
924 invokesync("change-authors");
927 jpcrr.ram_dump = function(name, binary)
928 local _name, _fname;
929 _name, _fname = toresourcename(name);
930 if binary then
931 invokesync("ram-dump-binary", {_name});
932 else
933 invokesync("ram-dump-text", {_name});
935 return _name;
938 jpcrr.hud.left_gap = function(f, g)
939 invoke("hud-left-gap", {toString(f), toString(g)});
942 jpcrr.hud.right_gap = function(f, g)
943 invoke("hud-right-gap", {toString(f), toString(g)});
946 jpcrr.hud.top_gap = function(f, g)
947 invoke("hud-top-gap", {toString(f), toString(g)});
950 jpcrr.hud.bottom_gap = function(f, g)
951 invoke("hud-bottom-gap", {toString(f), toString(g)});
954 jpcrr.hud.white_solid_box = function(f, x, y, w, h)
955 invoke("hud-white-solid-box", {toString(f), toString(x), toString(y), toString(w), toString(h)});
958 jpcrr.hud.box = function(f, x, y, w, h, t, lr, lg, lb, la, fr, fg, fb, fa)
959 invoke("hud-box", {toString(f), toString(x), toString(y), toString(w), toString(h),
960 toString(t), toString(lr), toString(lg), toString(lb), toString(la), toString(fr),
961 toString(fg), tostring(fb), toString(fa)});
964 jpcrr.hud.circle = function(f, x, y, r, t, lr, lg, lb, la, fr, fg, fb, fa)
965 invoke("hud-circle", {toString(f), toString(x), toString(y), toString(r),
966 toString(t), toString(lr), toString(lg), toString(lb), toString(la), toString(fr),
967 toString(fg), tostring(fb), toString(fa)});
970 jpcrr.hud.bitmap = function(f, x, y, bmap, lr, lg, lb, la, fr, fg, fb, fa)
971 invoke("hud-bitmap", {toString(f), toString(x), toString(y), bmap, toString(lr),
972 toString(lg), toString(lb), toString(la), toString(fr), toString(fg), tostring(fb),
973 toString(fa)});
976 jpcrr.hud.bitmap_binary = function(f, x, y, bmap, lr, lg, lb, la, fr, fg, fb, fa)
977 invoke("hud-bitmap-binary", {toString(f), toString(x), toString(y), bmap, toString(lr),
978 toString(lg), toString(lb), toString(la), toString(fr), toString(fg), tostring(fb),
979 toString(fa)});
982 jpcrr.set_pccontrol_pos = function(x, y)
983 invokesync("pccontrol-setwinpos", {toString(x), toString(y)});
986 jpcrr.set_luaplugin_pos = function(x, y)
987 invokesync("luaplugin-setwinpos", {toString(x), toString(y)});
990 jpcrr.set_pcmonitor_pos = function(x, y)
991 invokesync("pcmonitor-setwinpos", {toString(x), toString(y)});
994 jpcrr.set_pcstartstoptest_pos = function(x, y)
995 invokesync("pcstartstoptest-setwinpos", {toString(x), toString(y)});
998 jpcrr.set_virtualkeyboard_pos = function(x, y)
999 invokesync("virtualkeyboard-setwinpos", {toString(x), toString(y)});
1002 jpcrr.exit = function()
1003 invokesync("luaplugin-terminate");
1006 jpcrr.pc_start = function()
1007 invoke("pc-start");
1010 jpcrr.pc_stop = function()
1011 invokesync("pc-stop");
1014 jpcrr.screenshot = function(include_hud)
1015 if include_hud then
1016 invoke("screenshot-renderbuffer");
1017 else
1018 invoke("screenshot-vgabuffer");
1023 jpcrr.vretrace_start_trap = function(is_on)
1024 if is_on then
1025 invokesync("trap-vretrace-start-on");
1026 else
1027 invokesync("trap-vretrace-start-off");
1031 jpcrr.vretrace_end_trap = function(is_on)
1032 if is_on then
1033 invokesync("trap-vretrace-end-on");
1034 else
1035 invokesync("trap-vretrace-end-off");
1039 jpcrr.timed_trap = function(nsecs)
1040 if nsecs then
1041 invokesync("trap-timed", {toString(nsecs)});
1042 else
1043 invokesync("trap-timed-disable");
1047 jpcrr.write_byte = function(addr, value)
1048 invokesync("memory-write", {toString(addr), toString(value), "1"});
1051 jpcrr.write_word = function(addr, value)
1052 invokesync("memory-write", {toString(addr), toString(value), "2"});
1055 jpcrr.write_dword = function(addr, value)
1056 invokesync("memory-write", {toString(addr), toString(value), "4"});
1059 jpcrr.read_byte = function(addr)
1060 local t = {toString(addr), "1"};
1061 t = invokecall("memory-read", t);
1062 return (t or {})[1];
1065 jpcrr.read_word = function(addr)
1066 local t = {toString(addr), "2"};
1067 t = invokecall("memory-read", t);
1068 return (t or {})[1];
1071 jpcrr.read_dword = function(addr)
1072 local t = {toString(addr), "4"};
1073 t = invokecall("memory-read", t);
1074 return (t or {})[1];
1077 jpcrr.invoke = nil;
1078 jpcrr.invoke_synchronous = nil;
1079 jpcrr.call = null
1081 -- Dofile.
1082 dofile = function(_script)
1083 local chunk, err, indication
1084 chunk, err = loadfile(_script);
1085 if not chunk then
1086 error("Kernel: Can't load subscript " .. _script .. ": " .. err);
1088 return chunk();
1091 local args2 = args;
1092 args = null;
1093 args = {};
1094 for k, v in pairs(args2) do
1095 if (#k > 2 and string.byte(k, 1) == 120 and string.byte(k, 2) == 45) then
1096 args[string.sub(k, 3)] = v;
1099 jpcrr_raw = null;
1102 chunk = null;
1103 loaded, err = pcall(function()
1104 chunk, err = loadfile(script);
1105 if not chunk then
1106 error(err);
1108 end);
1109 if not loaded then
1110 print("Kernel: Can't load script " .. script .. ": " .. err);
1111 invoke("luaplugin-terminate");
1112 while true do end
1115 script = null;
1116 indication, err = pcall(chunk);
1117 if not indication then
1118 print("Kernel: Unprotected error in script: " .. err);
1119 invoke("luaplugin-terminate");
1120 while true do end