2 -- Copyright 2009-2010 Ilari Liusvaara
4 -- Licenced under GNU GPL v2.
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
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
24 -- - bit.any(number...)
25 -- Returns 48-bit number that has those bits set that are set in any of its
27 -- - bit.parity(number...)
28 -- Returns 48-bit number that has those bits set that are set in even number
30 -- - bit.any(number...)
31 -- Returns 48-bit number that has those bits set that are set in all of its
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
50 -- - bit.rol(number num, number shift)
51 -- Returns 48 rightmost bits of num rotated by shift places left. Going outside
52 -- shift range 0-47 produces unpredicatable results.
53 -- - bit.ror(number num, number shift)
54 -- Returns 48 rightmost bits of num rotated by shift places right. Going outside
55 -- shift range 0-47 produces unpredicatable results.
56 -- - bit.bswap2(number num)
57 -- Returns 16 rightmost bits of num byte-swapped.
58 -- - bit.bswap3(number num)
59 -- Returns 24 rightmost bits of num byte-swapped.
60 -- - bit.bswap4(number num)
61 -- Returns 32 rightmost bits of num byte-swapped.
62 -- - bit.bswap5(number num)
63 -- Returns 40 rightmost bits of num byte-swapped.
64 -- - bit.bswap6(number num)
65 -- Returns 48 rightmost bits of num byte-swapped.
66 -- - bit.signextend(number num, number bitnum)
67 -- Copy bitnum'th bit of num to all higher bits of num and return result. Going
68 -- outside bitnum range of 0-47 produces unpredictable results.
69 -- - bit.tosigned(number num, number bitnum)
70 -- Mask off bitnum'th bit and all higher bits. If bitnum'th bit of num was set,
71 -- perform 2s complement on number and negate the result.
72 -- - bit.tohex(number num)
73 -- Returns hexadecimal string representation of number.
74 -- - jpcrr.wait_event()
75 -- Waits for event. Returns event type and extra message (if present). Event
76 -- Types are "lock", "stop", "attach" and "message". Note that if you get event
77 -- of type "lock", you are now in frame hold.
78 -- - jpcrr.poll_event()
79 -- Same as wait_event(), but does not wait for event to occur (returns nil if there
81 -- - jpcrr.pc_running()
82 -- Returns true if PC is running.
83 -- - jpcrr.clock_time()
84 -- Returns current time or nil if no PC.
85 -- - jpcrr.pc_connected()
86 -- Returns true if PC is connected.
87 -- - jpcrr.in_frame_hold()
88 -- Returns true if in frame hold, false otherwise.
89 -- - jpcrr.keypressed(number key)
90 -- Return true if key is pressed, else false.
91 -- - jpcrr.keypressed_edge(number key)
92 -- Return true if key is pressed at input edge, else false.
93 -- - jpcrr.release_vga()
94 -- Allow VGA to exit frame hold mode. Wait for frame hold first.
95 -- - jpcrr.vga_resolution()
96 -- Return VGA x and y resolutions. -1x-1 or 0x0 is returned if no valid resolution.
97 -- Should only be called during frame hold.
98 -- - jpcrr.frame_number()
99 -- Return current VGA frame number or nil if no PC present.
100 -- - jpcrr.shutdown_emulator()
101 -- Shutdown the entiere emulator immediately (graceful shutdown, PCRunner only).
102 -- - jpcrr.hud.left_gap(number flags, number gap)
103 -- Set left gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
104 -- 1 (2) is set, dump to video dump.
105 -- - jpcrr.hud.right_gap(number flags, number gap)
106 -- Set right gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
107 -- 1 (2) is set, dump to video dump.
108 -- - jpcrr.hud.top_gap(number flags, number gap)
109 -- Set top gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
110 -- 1 (2) is set, dump to video dump.
111 -- - jpcrr.hud.bottom_gap(number flags, number gap)
112 -- Set bottom gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
113 -- 1 (2) is set, dump to video dump.
114 -- - jpcrr.hud.white_solid_box(number flags, number x, number y, number w, number h)
115 -- Draw with solid opaque box.
116 -- - jpcrr.hud.box(number flags, number x, number y, number w, number h, number linethick,
117 -- number lineRed, number lineGreen, number lineBlue, number lineAlpha,
118 -- number fillRed, number fillGreen, number fillBlue, number fillAlpha)
119 -- Draw box with specified size, border line thickness, line color and fill color.
120 -- - jpcrr.hud.circle(number flags, number x, number y, number r, number linethick,
121 -- number lineRed, number lineGreen, number lineBlue, number lineAlpha,
122 -- number fillRed, number fillGreen, number fillBlue, number fillAlpha)
123 -- Draw circle with specified size, border line thickness, line color and fill color.
124 -- - jpcrr.hud.bitmap(number flags, number x, number y, string bmap,
125 -- number fgRed, number fgGreen, number fgBlue, number fgAlpha,
126 -- number bgRed, number bgGreen, number bgBlue, number bgAlpha)
127 -- Draw bitmap with specified foreground color and background color.
128 -- - jpcrr.hud.bitmap_binary(number flags, number x, number y, string bmap,
129 -- number fgRed, number fgGreen, number fgBlue, number fgAlpha,
130 -- number bgRed, number bgGreen, number bgBlue, number bgAlpha)
131 -- Draw binary bitmap with specified foreground color and background color.
132 -- - jpcrr.hud.changen(number flags, number x, number y, string text,
133 -- number fgRed, number fgGreen, number fgBlue, number fgAlpha,
134 -- number bgRed, number bgGreen, number bgBlue, number bgAlpha,
135 -- boolean multiline)
136 -- Output string using chargen.
137 -- - jpcrr.events.count()
138 -- Return current event count. Nil if no PC.
139 -- - jpcrr.events.current_sequence()
140 -- Return sequence number of next event (event sequence numbers are 0-based). Nil if
141 -- no PC, -1 if at end of movie.
142 -- - jpcrr.events.by_sequence(number seq)
143 -- Return array for specified event. Nil if no PC, empty array if not valid sequence
144 -- number. Otherwise, first element of array is timestamp (numeric), the second is
145 -- event class and rest are the arguments.
146 -- - jpcrr.joystick_state()
147 -- Returns nil if no joystick. Otherwise returns hold times for all four axis (numeric)
148 -- followed by button states (boolean).
149 -- - jpcrr.keyboard_leds()
150 -- Returns nil if no keyboard, false if LED status is unknown. Otherwise returns three
151 -- booleans, first being state of num lock, second being state of caps lock and third
152 -- being state of scroll lock.
153 -- - jpcrr.mouse_state()
154 -- Returns nil if no mouse. Otherwise returns X, Y and Z axis pending motions (numeric)
155 -- followed by button states (5 booleans).
156 -- - jpcrr.component_encode(table components)
157 -- Return component encoding for specified components.
158 -- - jpcrr.component_decode(string line)
159 -- Return component decoding for specified line, nil/nil if it doesn't encode
160 -- anything, nil/string if parse error occurs (the error is the second return).
161 -- - jpcrr.save_state(string name)
162 -- Savestate into specified file. Returns name used.
163 -- - jpcrr.save_movie(string name)
164 -- Save movie into specified file. Returns name used.
165 -- - jpcrr.load_state_normal(string name)
166 -- Load specified savestate. Returns name used.
167 -- - jpcrr.load_state_preserve_events(string name)
168 -- Load specified savestate, preserving events. Returns name used.
169 -- - jpcrr.load_state_movie(string name)
170 -- Load specified savestate as movie. Returns name used.
171 -- - jpcrr.assemble()
172 -- Open system settings dialog.
173 -- - jpcrr.change_authors()
174 -- Open change authors dialog.
175 -- - jpcrr.exit = function()
177 -- - jpcrr.ram_dump(string name, boolean binary)
178 -- Dump PC memory to specified file. If binary is true, dump is binary, otherwise
179 -- textual hexadecimal dump.
180 -- - jpcrr.write_byte(number addr, number value)
181 -- Write byte to specified physical address.
182 -- - jpcrr.write_word(number addr, number value)
183 -- Write word to specified physical address (little endian).
184 -- - jpcrr.write_dword(number addr, number value)
185 -- Write dword to specified physical address (little endian).
186 -- - jpcrr.read_byte(number addr)
187 -- Return byte from specified physical address.
188 -- - jpcrr.read_word(number addr)
189 -- Return word from specified physical address (little endian).
190 -- - jpcrr.read_dword(number addr)
191 -- Return dword from specified physical address (little endian).
192 -- - jpcrr.read_byte_signed(number addr)
193 -- Return signed byte from specified physical address.
194 -- - jpcrr.read_word_signed(number addr)
195 -- Return signed word from specified physical address (little endian).
196 -- - jpcrr.read_dword_signed(number addr)
197 -- Return signed dword from specified physical address (little endian).
198 -- - jpcrr.timed_trap(number nsecs)
199 -- Set trap after specified number of nanoseconds. Use nil as nsecs to disable.
200 -- - jpcrr.vretrace_start_trap(boolean is_on)
201 -- Set trap on vretrace start on/off.
202 -- - jpcrr.vretrace_end_trap(boolean is_on)
203 -- Set trap on vretrace end on/off.
204 -- - jpcrr.pc_start()
205 -- Start PC execution.
207 -- Stop PC execution.
208 -- - jpcrr.set_pccontrol_pos(number x, number y)
209 -- Set position of PCControl window.
210 -- - jpcrr.set_luaplugin_pos(number x, number y)
211 -- Set position of LuaPlugin window.
212 -- - jpcrr.set_pcmonitor_pos(number x, number y)
213 -- Set position of PCMonitor window.
214 -- - jpcrr.set_pcstartstoptest_pos(number x, number y)
215 -- Set position of PCStartStopTest window.
216 -- - jpcrr.set_virtualkeyboard_pos(number x, number y)
217 -- Set position of VirtualKeyboard window.
218 -- - jpcrr.stringlessthan(String x, String y)
219 -- Return true if x is before y in codepoint lexical order, otherwise false.
220 -- - jpcrr.screenshot(boolean include_hud)
221 -- Take screen shot (Requires monitor). If include_hud is true, include HUD
222 -- (as shown on screen). Note that this should only be called during frame
223 -- hold or results are pretty much undefined.
224 -- - jpcrr.sendevent(string class, string/number...)
225 -- Sends specified event.
226 -- - jpcrr.sendevent_lowbound(number lowbound, string class, string/number...)
227 -- Sends specified event with specified time low bound.
228 -- - jpcrr.movie_rerecords()
229 -- Return number of rerecords (nil if no movie loaded).
230 -- - jpcrr.movie_length()
231 -- Return length of movie in ns (nil if no movie loaded).
232 -- - jpcrr.movie_headers()
233 -- Return headers of movie as array of arrays (nil if no movie loaded).
234 -- - jpcrr.register_redraw_function(function)
235 -- This function is called every time screen is redrawn. It is called with
236 -- VGA lock held and that lock is automatically released after call. If this
237 -- function has been registered, no lock events will be generated.
238 -- - jpcrr.register_message_function(function)
239 -- This function is called with message as parameter every time message is
240 -- received. Suppresses message events.
241 -- - jpcrr.register_attach_function(function)
242 -- This function is called every time attach is
243 -- received. Suppresses attach events.
244 -- - jpcrr.register_stop_function(function)
245 -- This function is called every time stop is
246 -- received. Suppresses stop events.
247 -- - jpcrr.register_uiaction_function(function)
248 -- This function is called with widget name as parameter every time
249 -- UI action event is received. Suppresses ui action events.
250 -- - jpcrr.stop_and_execute(function)
251 -- Stop PC execution and call specified function after PC has stopped
252 -- fully. Note that only handler functions receive events until this
255 -- I/O functions have the following conventions. If function returns any real data, the first
256 -- return value returns this data or is nil. Otherwise first return value is true or false.
257 -- If first return value is nil or false, then the second return value gives textual error
258 -- message for failed operation, or is nil if EOF occured before anything was read.
260 -- Unlink, rename and mkdir don't follow this pattern. They just return true/false to signal
261 -- success or failure.
263 -- Specifying nil as name of file results random filename being used (it even works with unlink,
264 -- mkdir, rename and read-only access, but doesn't make any sense there).
266 -- Specifying empty filename prompts for file (doesn't work with mkdir, rename nor unlink). Use
267 -- '/<title>' to specify title for prompt dialog.
269 -- Class: BinaryFile:
270 -- Binary file for RO or RW access. Methods are as follows:
272 -- Return name of file.
274 -- Return length of file.
275 -- - set_length(number length)
276 -- Truncate file to specified length
277 -- - read(number offset, number length)
278 -- Read up to length bytes from offset.
279 -- - write(number offset, string content)
280 -- Write content to specified offset.
283 -- Class: BinaryInput:
284 -- Binary file for sequential input. Methods are as follows:
286 -- Return name of file.
288 -- Return BinaryInput that is four to five decoding of this stream.
290 -- Return stream as TextInput.
292 -- Return BinaryInput that is inflate of this stream.
293 -- - read(number bytes)
294 -- Read up to bytes bytes from file.
296 -- Read the entiere file at once.
299 -- Character set for binary files is Latin-1.
301 -- Class: BinaryOutput:
302 -- Binary file for sequential output. Methods are as follows:
304 -- Return name of file.
306 -- Return BinaryOutput that writes four to five encoded output to this stream.
308 -- Return stream as TextOutput.
310 -- Return BinaryOutput that writes deflate output to this stream.
311 -- - write(string content)
312 -- Write string to file.
315 -- Character set for binary files is Latin-1.
319 -- Return name of file.
321 -- Read line from file.
322 -- - read_component()
323 -- Read next componented line into array.
325 -- Line iterator function.
328 -- Character set for text files is UTF-8.
330 -- Class: TextOutput:
332 -- Return name of file.
333 -- - write(string line)
334 -- Write line line to file.
335 -- - write_component(table components)
336 -- Write componented line.
339 -- Character set for text files is UTF-8.
342 -- - member(string name)
343 -- Open substream for member name. The methods are the same as for io.opentextin.
344 -- - member_binary(string name)
345 -- Open binary (four to five) substream for member name. The methods are the same as
346 -- for io.openbinaryin.
348 -- Close the file. Any opened substreams are invalidated.
350 -- Return table listing all members of archive (sorted by name).
352 -- - member(string name)
353 -- Open substream for member name. The methods are the same as for io.opentextout. Note that
354 -- previous member must be closed before next can open.
355 -- - member_binary(string name)
356 -- Open binary (four to five) substream for member name. The methods are the same as
357 -- for io.openbinaryout. Note that previous substream must be closed before next can open.
359 -- Commit the file. No substream may be open. Closes the file.
361 -- Rollback the file. No substream may be open. Closes the file.
363 -- - io.open(string name, string mode) -> BinaryFile
364 -- Open file named @name in specified mode. The mode can be 'r' (read only) or 'rw' (read and
366 -- - io.open_read(string name) -> BinaryInput
367 -- Open file named @name as binary input stream.
368 -- - io.open_write(string name) -> BinaryOutput
369 -- Open file named @name as binary input stream.
370 -- - io.open_arch_read(string name) -> ArchiveIn
371 -- Open file named @name as input archive.
372 -- - io.open_arch_write(string name) -> ArchiveOut
373 -- Open file named @name as output archive.
374 -- - io.mkdir(string name)
375 -- Create directory name. Returns name created on success, nil on failure.
376 -- - io.unlink(string name)
377 -- Delete file/directory name. Returns name deleted on success, nil on failure.
378 -- - io.rename(string old, string new)
379 -- Rename file old -> new. Returns old, new on success, nil on failure.
380 -- - io.transform.text()
381 -- Returns function that calls text() method of its parameter.
382 -- - io.transform.four_to_five()
383 -- Returns function that calls four_to_five() method of its parameter.
384 -- - io.transform.deflate()
385 -- Returns function that calls deflate() method of its parameter.
386 -- - io.transform.inflate()
387 -- Returns function that calls inflate() method of its parameter.
388 -- - io.dotransform(object obj, function...)
389 -- Call specified functions on specified object. If any function fails (first argument nil
390 -- or false), call close method on preceeding object. Otherwise call specified functions
391 -- chained left to right and return the result.
392 -- - io.dotransform2(object obj, string err, function...)
393 -- Similar to dotransform except if obj is nil or false, returns obj, err.
396 local stopping = false;
397 local mesage_function = nil;
398 local lock_function = nil;
399 local uiaction_function = nil;
400 local stop_function = nil;
401 local attach_function = nil;
403 local handle, err, chunk, indication, k, v;
405 local loadmod = loadmodule;
408 local export_module_in = function(tab, modname, prefix)
409 local fun = loadmod(modname);
410 for k, v in pairs(fun) do
411 tab[(prefix or "") .. k] = v;
419 jpcrr.memorysearch = {};
423 export_module_in(jpcrr, "org.jpc.luaextensions.Base");
424 export_module_in(jpcrr, "org.jpc.luaextensions.InputDevices");
425 export_module_in(jpcrr, "org.jpc.luaextensions.ComponentCoding", "component_");
426 export_module_in(jpcrr.events, "org.jpc.luaextensions.Events");
427 export_module_in(jpcrr.window, "org.jpc.luaextensions.Window");
428 export_module_in(bit, "org.jpc.luaextensions.Bitops");
429 export_module_in(jpcrr.memorysearch, "org.jpc.luaextensions.MemorySearch");
431 -- Few misc functions.
432 assert = function(val, err)
433 if (not val) and err then
439 modulus_split = function(number, ...)
440 local dividers = {...};
444 for k, v in ipairs(dividers) do
446 table.insert(results, (number - rem) / v);
450 table.insert(results, number);
451 return unpack(results);
454 jpcrr.register_redraw_function = function(f)
455 if f and type(f) ~= "function" then
456 error("Incorrect type of argument to register_redraw_function");
461 jpcrr.register_message_function = function(f)
462 if f and type(f) ~= "function" then
463 error("Incorrect type of argument to register_message_function");
465 message_function = f;
468 jpcrr.register_stop_function = function(f)
469 if f and type(f) ~= "function" then
470 error("Incorrect type of argument to register_stop_function");
475 jpcrr.register_attach_function = function(f)
476 if f and type(f) ~= "function" then
477 error("Incorrect type of argument to register_attach_function");
482 jpcrr.register_uiaction_function = function(f)
483 if f and type(f) ~= "function" then
484 error("Incorrect type of argument to register_uiaction_function");
486 uiaction_function = f;
491 local getmtable = getmetatable;
492 local toString = tostring;
493 local inject_binary_file;
494 local inject_binary_input;
495 local inject_binary_output;
496 local inject_text_input;
497 local inject_text_output;
498 local inject_archive_input;
499 local inject_archive_output;
501 -- Class member injectors.
502 inject_binary_file = function(obj, name)
504 getmtable(obj).name = function(obj)
507 getmtable(obj).__index = function(tab, name)
508 local x = getmtable(obj)[name];
512 error("Invalid method " .. name .. " for BinaryFile");
517 inject_binary_input = function(obj, underlying, name)
519 local old_four_to_five = getmtable(obj).four_to_five;
520 local old_inflate = getmtable(obj).inflate;
521 local old_text = getmtable(obj).text;
522 local old_read = getmtable(obj).read;
523 local old_close = getmtable(obj).close;
524 local underlying_object = underlying;
526 getmtable(obj).name = function(obj)
529 getmtable(obj).four_to_five = function(obj)
531 res, err = old_four_to_five(obj);
535 return inject_binary_input(res, obj, "four-to-five<" .. _name .. ">");
537 getmtable(obj).inflate = function(obj)
539 res, err = old_inflate(obj);
543 return inject_binary_input(res, obj, "inflate<" .. _name .. ">");
545 getmtable(obj).text = function(obj)
547 res, err = old_text(obj);
551 return inject_text_input(res, obj, "text<" .. _name .. ">");
553 getmtable(obj).read = function(obj, toread)
555 return old_read(obj, toread);
560 ret, err = old_read(obj, 16384);
571 getmtable(obj).close = function(obj)
572 local ret, err, ret2, err2;
573 ret, err = old_close(obj);
574 if underlying_object then ret2, err2 = underlying_object:close(); end
575 if ret and not ret2 then
581 getmtable(obj).__index = function(tab, name)
582 local x = getmtable(obj)[name];
586 error("Invalid method " .. name .. " for BinaryInput");
591 inject_binary_output = function(obj, underlying, name)
593 local old_four_to_five = getmtable(obj).four_to_five;
594 local old_deflate = getmtable(obj).deflate;
595 local old_text = getmtable(obj).text;
596 local old_close = getmtable(obj).close;
597 local underlying_object = underlying;
599 getmtable(obj).name = function(obj)
602 getmtable(obj).four_to_five = function(obj)
604 res, err = old_four_to_five(obj);
608 return inject_binary_output(res, obj, "four-to-five<" .. _name .. ">");
610 getmtable(obj).deflate = function(obj)
612 res, err = old_deflate(obj);
616 return inject_binary_output(res, obj, "deflate<" .. _name .. ">");
618 getmtable(obj).text = function(obj)
620 res, err = old_text(obj);
624 return inject_text_output(res, obj, "text<" .. _name .. ">");
626 getmtable(obj).close = function(obj)
627 local ret, err, ret2, err2;
628 ret, err = old_close(obj);
629 if underlying_object then ret2, err2 = underlying_object:close(); end
630 if ret and not ret2 then
636 getmtable(obj).__index = function(tab, name)
637 local x = getmtable(obj)[name];
641 error("Invalid method " .. name .. " for BinaryOutput");
646 inject_text_input = function(obj, underlying, name)
648 local old_close = getmtable(obj).close;
649 local underlying_object = underlying;
651 getmtable(obj).lines = function(obj)
652 return function(state, prevline)
656 getmtable(obj).name = function(obj)
659 getmtable(obj).close = function(obj)
660 local ret, err, ret2, err2;
661 ret, err = old_close(obj);
662 if underlying_object then ret2, err2 = underlying_object:close(); end
663 if ret and not ret2 then
669 getmtable(obj).__index = function(tab, name)
670 local x = getmtable(obj)[name];
674 error("Invalid method " .. name .. " for TextInput");
679 inject_text_output = function(obj, underlying, name)
681 local old_close = getmtable(obj).close;
682 local underlying_object = underlying;
684 getmtable(obj).name = function(obj)
687 getmtable(obj).close = function(obj)
688 local ret, err, ret2, err2;
689 ret, err = old_close(obj);
690 if underlying_object then ret2, err2 = underlying_object:close(); end
691 if ret and underlying_object then
697 getmtable(obj).__index = function(tab, name)
698 local x = getmtable(obj)[name];
702 error("Invalid method " .. name .. " for TextOutput");
707 inject_archive_input = function(obj, name)
709 local old_member = getmtable(obj).member;
710 local old_member_list = getmtable(obj).member_list;
711 getmtable(obj).member = function(obj, member)
713 res, err = old_member(obj, member);
717 return inject_binary_input(res, nil, _name .. "[" .. member .. "]");
719 getmtable(obj).name = function(obj)
722 getmtable(obj).member_list = function(obj)
723 local tab = old_member_list(obj);
724 if tab then table.sort(tab, jpcrr.stringlessthan); end
727 getmtable(obj).__index = function(tab, name)
728 local x = getmtable(obj)[name];
732 error("Invalid method " .. name .. " for ArchiveInput");
737 inject_archive_output = function(obj, name)
739 local old_member = getmtable(obj).member;
740 getmtable(obj).member = function(obj, member)
742 res, err = old_member(obj, member);
746 return inject_binary_output(res, nil, _name .. "[" .. member .. "]");
748 getmtable(obj).name = function(obj)
751 getmtable(obj).__index = function(tab, name)
752 local x = getmtable(obj)[name];
756 error("Invalid method " .. name .. " for ArchiveOutput");
762 -- Redefined routines.
764 local rprint = print_console_msg;
765 print_console_msg = nil;
766 print = function(...)
772 x = x .. "\t" .. toString(y[i]);
779 print_console_msg = nil;
785 local stringfind = string.find;
786 local randname = loadmod("org.jpc.luaextensions.DelayedDelete").random_temp_name;
787 local selectname = loadmod("org.jpc.luaextensions.BaseFSOps").opensave_dialog;
788 local path = args["luapath"] or ".";
789 local toresourcename = function(resname, save, text)
791 return randname(path .. "/", "luatemp-");
794 if resname == "" and text then
796 a, b = selectname(save, text);
800 if stringfind(resname, "^/") then
802 error("Bad resource name (case 2): " .. resname);
805 a, b = selectname(save, string.sub(resname, 2));
809 if not stringfind(resname, "[%d%l%u_%-]") then
810 error("Bad resource name (case 1): " .. resname);
812 if stringfind(resname, "%.%.") then
813 error("Bad resource name (case 3): " .. resname);
815 if stringfind(resname, "\\") then
816 error("Bad resource name (case 4): " .. resname);
819 return resname, path .. "/" .. resname;
824 local openbinin = loadmod("org.jpc.luaextensions.BinaryInFile").open;
825 local openbinout = loadmod("org.jpc.luaextensions.BinaryOutFile").open;
826 local openarchin = loadmod("org.jpc.luaextensions.ArchiveIn").open;
827 local openarchout = loadmod("org.jpc.luaextensions.ArchiveOut").open;
828 local openbinary = loadmod("org.jpc.luaextensions.BinaryFile").open;
830 local baseFS = loadmod("org.jpc.luaextensions.BaseFSOps");
831 local mkdir = baseFS.mkdir;
832 local unlink = baseFS.unlink;
833 local rename = baseFS.rename;
835 local getmtable = getmetatable;
837 loadfile = function(_script)
838 local file, file2, err, content;
840 x, y = toresourcename(_script, false, "Select script to load");
841 if not x then return x, y; end
842 file, err = openbinin(y, "r");
844 return nil, "Can't open " .. _script .. ": " .. err;
846 file2, err = file:text();
848 return nil, "Can't transform " .. _script .. ": " .. err;
853 content = content .. line .. "\n";
858 return loadstring(content, _script);
861 io.open = function(name, mode)
866 _name, y = toresourcename(name, false, "Select file to read");
868 _name, y = toresourcename(name, true, "Select file to write");
870 if not _name then return _name, y; end
871 res, err = openbinary(y, mode);
875 return inject_binary_file(res, _name);
878 io.open_arch_read = function(name)
882 _name, y = toresourcename(name, false, "Select archive to read");
883 if not _name then return _name, y; end
884 res, err = openarchin(y);
888 return inject_archive_input(res, _name);
891 io.open_arch_write = function(name)
895 _name, y = toresourcename(name, true, "Select archive to write");
896 if not _name then return _name, y; end
897 res, err = openarchout(y);
901 return inject_archive_output(res, _name);
904 io.open_read = function(name)
908 _name, y = toresourcename(name, false, "Select file to read");
909 if not _name then return _name, y; end
910 res, err = openbinin(y);
914 return inject_binary_input(res, nil, _name);
917 io.open_write = function(name)
921 _name, y = toresourcename(name, true, "Select file to write");
922 if not _name then return _name, y; end
923 res, err = openbinout(y);
927 return inject_binary_output(res, nil, _name);
930 io.mkdir = function(name)
932 _name, y = toresourcename(name);
940 io.unlink = function(name)
942 _name, y = toresourcename(name);
950 io.rename = function(name1, name2)
953 _name, y = toresourcename(name1);
954 _name2, y2 = toresourcename(name2);
955 if rename(y, y2) then
956 return _name, _name2;
964 io.transform.text = function()
970 io.transform.four_to_five = function()
972 return obj:four_to_five();
976 io.transform.inflate = function()
978 return obj:inflate();
982 io.transform.deflate = function()
984 return obj:deflate();
988 io.dotransform = function(obj, ...)
992 for k, v in ipairs(todo) do
1003 io.dotransform2 = function(obj, err, ...)
1007 return io.dotransform(obj, err, ...);
1012 -- Various stuff built on top of ECI.
1013 local invoke = jpcrr.invoke;
1014 local invokecall = jpcrr.call;
1015 local invokesync = jpcrr.invoke_synchronous;
1017 jpcrr.sendevent = function(...)
1018 local arguments = {...};
1020 for k, v in ipairs(arguments) do
1021 arguments[k] = toString(v);
1023 invokesync("sendevent", arguments);
1026 jpcrr.sendevent_lowbound = function(...)
1027 local arguments = {...};
1029 for k, v in ipairs(arguments) do
1030 arguments[k] = toString(v);
1032 invokesync("sendevent-lowbound", arguments);
1035 jpcrr.save_state = function(name)
1036 local _name, _fname;
1037 _name, _fname = toresourcename(name, true, "File to savestate to");
1038 invokesync("state-save", {_fname});
1042 jpcrr.save_movie = function(name)
1043 local _name, _fname;
1044 _name, _fname = toresourcename(name, true, "File to save movie to");
1045 invokesync("movie-save", {_fname});
1049 jpcrr.load_state_normal = function(name)
1050 local _name, _fname;
1051 _name, _fname = toresourcename(name, false, "File to loadstate from");
1052 invokesync("state-load", {_fname});
1056 jpcrr.load_state_preserve_events = function(name)
1057 local _name, _fname;
1058 _name, _fname = toresourcename(name, false, "File to loadstate (preserve events) from");
1059 invokesync("state-load-noevents", {_fname});
1063 jpcrr.load_state_movie = function(name)
1064 local _name, _fname;
1065 _name, _fname = toresourcename(name, false, "File to load movie from");
1066 invokesync("movie-load", {_fname});
1070 jpcrr.assemble = function()
1071 invokesync("pc-assemble");
1074 jpcrr.change_authors = function()
1075 invokesync("change-authors");
1078 jpcrr.ram_dump = function(name, binary)
1079 local _name, _fname;
1080 _name, _fname = toresourcename(name, true, "Select file to ramdump to");
1082 invokesync("ram-dump-binary", {_fname});
1084 invokesync("ram-dump-text", {_fname});
1089 local poll_event = jpcrr.poll_event;
1090 local _print = print;
1091 local _unlock = jpcrr.release_vga;
1092 jpcrr.poll_event = function()
1094 local failed = false;
1096 a, b = poll_event();
1100 if a and a == "message" and message_function then
1101 a, b = pcall(message_function, b);
1103 _print("Error running message callback: " .. b);
1107 if a and a == "uiaction" and uiaction_function then
1108 a, b = pcall(uiaction_function, b);
1110 _print("Error running uiaction callback: " .. b);
1114 if a and a == "stop" and stop_function then
1115 a, b = pcall(stop_function);
1117 _print("Error running stop callback: " .. b);
1121 if a and a == "attach" and attach_function then
1122 a, b = pcall(attach_function);
1124 _print("Error running attach callback: " .. b);
1128 if a and a == "lock" and lock_function then
1129 a, b = pcall(lock_function);
1132 _print("Error running redraw callback: " .. b);
1139 if a and a == "lock" and stopping then
1148 local wait_event = jpcrr.wait_event;
1149 local _print = print;
1150 local _unlock = jpcrr.release_vga;
1151 jpcrr.wait_event = function()
1154 a, b = wait_event();
1155 if a and a == "message" and message_function then
1156 a, b = pcall(message_function, b);
1158 _print("Error running message callback: " .. b);
1162 if a and a == "uiaction" and uiaction_function then
1163 a, b = pcall(uiaction_function, b);
1165 _print("Error running uiaction callback: " .. b);
1169 if a and a == "stop" and stop_function then
1170 a, b = pcall(stop_function);
1172 _print("Error running stop callback: " .. b);
1176 if a and a == "attach" and attach_function then
1177 a, b = pcall(attach_function);
1179 _print("Error running attach callback: " .. b);
1183 if a and a == "lock" and lock_function then
1184 a, b = pcall(lock_function);
1187 _print("Error running redraw callback: " .. b);
1199 jpcrr.hud.left_gap = function(f, g)
1200 invoke("hud-left-gap", {toString(f), toString(g)});
1203 jpcrr.hud.right_gap = function(f, g)
1204 invoke("hud-right-gap", {toString(f), toString(g)});
1207 jpcrr.hud.top_gap = function(f, g)
1208 invoke("hud-top-gap", {toString(f), toString(g)});
1211 jpcrr.hud.bottom_gap = function(f, g)
1212 invoke("hud-bottom-gap", {toString(f), toString(g)});
1215 jpcrr.hud.white_solid_box = function(f, x, y, w, h)
1216 invoke("hud-white-solid-box", {toString(f), toString(x), toString(y), toString(w), toString(h)});
1219 jpcrr.hud.box = function(f, x, y, w, h, t, lr, lg, lb, la, fr, fg, fb, fa)
1220 invoke("hud-box", {toString(f), toString(x), toString(y), toString(w), toString(h),
1221 toString(t), toString(lr), toString(lg), toString(lb), toString(la), toString(fr),
1222 toString(fg), tostring(fb), toString(fa)});
1225 jpcrr.hud.circle = function(f, x, y, r, t, lr, lg, lb, la, fr, fg, fb, fa)
1226 invoke("hud-circle", {toString(f), toString(x), toString(y), toString(r),
1227 toString(t), toString(lr), toString(lg), toString(lb), toString(la), toString(fr),
1228 toString(fg), tostring(fb), toString(fa)});
1231 jpcrr.hud.bitmap = function(f, x, y, bmap, lr, lg, lb, la, fr, fg, fb, fa)
1232 invoke("hud-bitmap", {toString(f), toString(x), toString(y), bmap, toString(lr),
1233 toString(lg), toString(lb), toString(la), toString(fr), toString(fg), tostring(fb),
1237 jpcrr.hud.bitmap_binary = function(f, x, y, bmap, lr, lg, lb, la, fr, fg, fb, fa)
1238 invoke("hud-bitmap-binary", {toString(f), toString(x), toString(y), bmap, toString(lr),
1239 toString(lg), toString(lb), toString(la), toString(fr), toString(fg), tostring(fb),
1243 jpcrr.hud.chargen = function(f, x, y, text, lr, lg, lb, la, fr, fg, fb, fa, multiline)
1244 invoke("hud-vga-chargen", {toString(f), toString(x), toString(y), text, toString(lr),
1245 toString(lg), toString(lb), toString(la), toString(fr), toString(fg), tostring(fb),
1246 toString(fa), toString(multiline and 1 or 0)});
1249 jpcrr.shutdown_emulator = function()
1250 invokesync("shutdown-emulator", {});
1253 jpcrr.set_pccontrol_pos = function(x, y)
1254 invokesync("pccontrol-setwinpos", {toString(x), toString(y)});
1257 jpcrr.set_luaplugin_pos = function(x, y)
1258 invokesync("luaplugin-setwinpos", {toString(x), toString(y)});
1261 jpcrr.set_pcmonitor_pos = function(x, y)
1262 invokesync("pcmonitor-setwinpos", {toString(x), toString(y)});
1265 jpcrr.set_pcstartstoptest_pos = function(x, y)
1266 invokesync("pcstartstoptest-setwinpos", {toString(x), toString(y)});
1269 jpcrr.set_virtualkeyboard_pos = function(x, y)
1270 invokesync("virtualkeyboard-setwinpos", {toString(x), toString(y)});
1273 jpcrr.exit = function()
1274 invokesync("luaplugin-terminate");
1277 jpcrr.pc_start = function()
1281 jpcrr.pc_stop = function()
1282 invokesync("pc-stop");
1285 jpcrr.screenshot = function(include_hud)
1287 invoke("screenshot-renderbuffer");
1289 invoke("screenshot-vgabuffer");
1294 jpcrr.vretrace_start_trap = function(is_on)
1296 invokesync("trap-vretrace-start-on");
1298 invokesync("trap-vretrace-start-off");
1302 jpcrr.vretrace_end_trap = function(is_on)
1304 invokesync("trap-vretrace-end-on");
1306 invokesync("trap-vretrace-end-off");
1310 jpcrr.timed_trap = function(nsecs)
1312 invokesync("trap-timed", {toString(nsecs)});
1314 invokesync("trap-timed-disable");
1318 jpcrr.write_byte = function(addr, value)
1319 invokesync("memory-write", {toString(addr), toString(value), "1"});
1322 jpcrr.write_word = function(addr, value)
1323 invokesync("memory-write", {toString(addr), toString(value), "2"});
1326 jpcrr.write_dword = function(addr, value)
1327 invokesync("memory-write", {toString(addr), toString(value), "4"});
1330 jpcrr.read_byte = function(addr)
1331 local t = {toString(addr), "1"};
1332 t = invokecall("memory-read", t);
1333 return (t or {})[1];
1336 jpcrr.read_word = function(addr)
1337 local t = {toString(addr), "2"};
1338 t = invokecall("memory-read", t);
1339 return (t or {})[1];
1342 jpcrr.read_dword = function(addr)
1343 local t = {toString(addr), "4"};
1344 t = invokecall("memory-read", t);
1345 return (t or {})[1];
1348 jpcrr.read_byte_signed = function(addr)
1349 local t = {toString(addr), "1"};
1350 t = invokecall("memory-read", t);
1351 return bit.tosigned((t or {})[1], 7);
1354 jpcrr.read_word_signed = function(addr)
1355 local t = {toString(addr), "2"};
1356 t = invokecall("memory-read", t);
1357 return bit.tosigned((t or {})[1], 15);
1360 jpcrr.read_dword_signed = function(addr)
1361 local t = {toString(addr), "4"};
1362 t = invokecall("memory-read", t);
1363 return bit.tosigned((t or {})[1], 31);
1366 local e_wait_event = jpcrr.wait_event;
1367 local running = jpcrr.pc_running;
1368 local stop_and_execute_in_progress = false;
1369 jpcrr.stop_and_execute = function(f)
1372 if stop_and_execute_in_progress then
1375 if not running() then
1378 error("Error in after stop callback: " .. b);
1384 stop_and_execute_in_progress = true;
1385 a, b = e_wait_event();
1386 stop_and_execute_in_progress = false;
1387 if a and a == "lock" then
1392 elseif a and (a == "stop" or a == "attach") then
1399 error("Error in after stop callback: " .. b);
1406 jpcrr.invoke_synchronous = nil;
1410 dofile = function(_script)
1411 local chunk, err, indication
1412 chunk, err = loadfile(_script);
1414 error("Kernel: Can't load subscript " .. _script .. ": " .. err);
1422 for k, v in pairs(args2) do
1423 if (#k > 2 and string.byte(k, 1) == 120 and string.byte(k, 2) == 45) then
1424 args[string.sub(k, 3)] = v;
1431 loaded, err = pcall(function()
1432 chunk, err = loadfile(script);
1438 print("Kernel: Can't load script " .. script .. ": " .. err);
1439 invoke("luaplugin-terminate");
1444 indication, err = pcall(chunk);
1445 if not indication then
1446 print("Kernel: Unprotected error in script: " .. err);
1447 invoke("luaplugin-terminate");