Allow nullary BinaryInFile::read() in Lua
[jpcrr.git] / manual.txt
blobe949e7c2fdbb9f0e55ffffdf31a39c4ab1fe0da2
1 JPC-RR: User's manual
3 1 Licence
5 JPC-RR is licenced under GNU GPL v2. See file “LICENSE”
7 2 Getting started
9 2.1 Prerequisites
11 To get started, you need BIOS image, VGABIOS image and DOS boot 
12 floppy and JDK for Java 6 standard edition (later versions should 
13 they appear should also work). Note: JRE is not enough. 
15 Note that to play back recorded movies, you need exact same 
16 version of BIOS image, VGABIOS image and DOS boot floppy as was 
17 used when making the movie (in addition to exact same versions of 
18 other needed media).
20 See compile.sh. The stuff in streamtools is only needed for 
21 recording videos.
23 2.2 Compiling
25 See compile.sh. The streamtools stuff is only needed for dumping 
26 videos.
28 2.3 Getting started
30 First you need to get and make some important images. Obtain BIOS 
31 image, VGABIOS image and DOS boot floppy from somewhere and do:
33 mkdir library
35 java ImageMaker --BIOS library/BIOS <bios image file>
37 java ImageMaker --BIOS library/VGABIOS <vgabios image file>
39 java ImageMaker --floppy1440 library/DOSfloppy <dos floppy name>
41 This makes loadable images out of what you obtained. Most DOS 
42 boot floppies are 1440KiB in size (if yours isn't change that 
43 floppy1440 part). You also might want to make some game images:
45 java ImageMaker --HDD=128,63,16 library/somegame <game 
46 installation directory>
48 This makes ~128MB HDD image out of installation directory.
50 2.4 Running emulator
52 There is premade autoexec script called assemble.bat that has 
53 fairly reasonable defaults. To use it:
55 java JPCApplication -library library -autoexec assemble.bat
57 The “-library library” specifies that contents of directory 
58 'library' are to be used as library. The script pops up settings 
59 for new emulated PC (if you want to load savestate, click 
60 cancel). Select BIOS and VGABIOS for BIOS and VGABIOS image (they 
61 should be already selected), DOSfloppy for fda (boot device 
62 should be set to fda) and game image as some HD drive 
64 2.5 Bootup tips
66 • Putting the game as hdd causes boot to be bit faster.
68 • Some BIOS versions have “press F12 to select boot device”. Hit 
69   <enter> from emulated keyboard and that prompt will go away in 
70   about half emulated second (it stays several emulated seconds 
71   otherwise). 
73 • If game doesn't need lots of memory, hitting F5 to skip 
74   intialization files is fastest. If it does need more memory, 
75   run config.sys commands but not autoexec.bat. 
77 • Some DOS disks have DOSIDLE with them, don't use it as it 
78   messes badly with emulator.
80 3 Making JPC-RR format images from raw images
82 Due to various factors, JPC-RR can't use raw image files directly 
83 but requires its own image format. There is tool called 
84 ImageMaker that can make JPC-RR images from raw images. Each 
85 image has format, ID an name. Format and name are specified when 
86 making image. ID is automatically calculated from format and 
87 contents. Name does not affect the ID but is purely for convience 
88 so one doesn't have to specify long image IDs manually.
90 3.1 Syntax
92 The syntax for ImageMaker when making images is:
94 $ java ImageMaker <format> [<options>...] <destination> <source> 
95 <name>
97 <destination> is file name for JPC-RR format image to write. 
98 <source> is either name of regular file (raw image file) or name 
99 of directory tree with files (supported for making floppy or hard 
100 disk images only). In case of directory tree, the files are 
101 layout deterministically to disk, so the ID will always be the 
102 same for given geometry and type. <name> is name to give to disk. 
103 <format> is one of:
105 --BIOS BIOS image (note: VGABIOS is also of this type).
107 --CDROM CD-ROM image.
109 --HDD=cylinders,sectors,heads Hard disk with specified geometry.
111 --floppy=tracks,sectors,sides Floppy disk with specified 
112 geometry.
114 --floppy160 160KiB floppy (40 tracks, 8 sectors, Single sided).
116 --floppy180 180KiB floppy (40 tracks, 9 sectors, Single sided).
118 --floppy320 320KiB floppy (40 tracks, 8 sectors, Double sided).
120 --floppy360 360KiB floppy (40 tracks, 9 sectors, Double sided).
122 --floppy410 410KiB floppy (41 tracks, 10 sectors, Double sided).
124 --floppy420 420KiB floppy (42 tracks, 10 sectors, Double sided).
126 --floppy720 720KiB floppy (80 tracks, 9 sectors, Double sided).
128 --floppy800 800KiB floppy (80 tracks, 10 sectors, Double sided).
130 --floppy820 820KiB floppy (82 tracks, 10 sectors, Double sided).
132 --floppy830 830KiB floppy (83 tracks, 10 sectors, Double sided).
134 --floppy880 880KiB floppy (80 tracks, 11 sectors, Double sided).
136 --floppy1040 1040KiB floppy (80 tracks, 13 sectors, Double 
137 sided).
139 --floppy1120 1120KiB floppy (80 tracks, 14 sectors, Double 
140 sided).
142 --floppy1200 1200KiB floppy (80 tracks, 15 sectors, Double 
143 sided).
145 --floppy1440 1440KiB floppy (80 tracks, 18 sectors, Double 
146 sided).
148 --floppy1476 1476KiB floppy (82 tracks, 18 sectors, Double 
149 sided).
151 --floppy1494 1494KiB floppy (83 tracks, 18 sectors, Double 
152 sided).
154 --floppy1600 1600KiB floppy (80 tracks, 20 sectors, Double 
155 sided).
157 --floppy1680 1680KiB floppy (80 tracks, 21 sectors, Double 
158 sided).
160 --floppy1722 1722KiB floppy (82 tracks, 21 sectors, Double 
161 sided).
163 --floppy1743 1743KiB floppy (83 tracks, 21 sectors, Double 
164 sided).
166 --floppy1760 1760KiB floppy (80 tracks, 22 sectors, Double 
167 sided).
169 --floppy1840 1840KiB floppy (80 tracks, 23 sectors, Double 
170 sided).
172 --floppy1920 1920KiB floppy (80 tracks, 24 sectors, Double 
173 sided).
175 --floppy2880 2880KiB floppy (80 tracks, 36 sectors, Double 
176 sided).
178 --floppy3120 3120KiB floppy (80 tracks, 39 sectors, Double 
179 sided).
181 --floppy3200 3200KiB floppy (80 tracks, 40 sectors, Double 
182 sided).
184 --floppy3520 3520KiB floppy (80 tracks, 44 sectors, Double 
185 sided).
187 --floppy3840 3840KiB floppy (80 tracks, 48 sectors, Double 
188 sided).
190 3.2 Notes
192 • If making image from directory, the names of the files must 
193   conform to FAT naming restrictions (8+3 character names, no 
194   spaces, etc). Avoid filenames with non-ASCII characters. 
196 • The DOS limit of 112 or 224 files for floppies does not apply 
197   to images created from directory trees. The minimum limit value 
198   used is 512. If even that isn't enough, the limit is 
199   automatically increased to fit all the needed directory 
200   entries.
202 • Making boot disks from tree does NOT work. Even if you got 
203   system boot files there, it still won't work.
205 • Only floppy disks and hard drives can be made from directory 
206   trees. BIOS images and CDROM images require image file.
208 • Avoid floppies with custom geometry (floppy geometry does 
209   affect disk ID). Disks with over 63 sectors per track don't 
210   work with DOS. Wheither disks with over 127 tracks per side 
211   work with DOS is unknown. Also avoid 1024-tracks per side HDDs.
213 • The geometry limits are: 2-1024 tracks per side for HDD, 1-256 
214   tracks per side for floppy. 1-63 sectors per track for HDD, 
215   1-255 sectors per track for floppy. 1-16 sides for HDD, 1 or 2 
216   sides for floppy. This gives size limit of 65280KiB for floppy 
217   disks (but note the DOS limit!) and 516096KiB for HDDs.
219 • There are multiple image file contents that represent the same 
220   image. The one with smallest size is picked when creating 
221   image.
223 • Note: Although the IDs are 128 bits long, they are not MD5 
224   hashes. 
226 3.3 Other options
228 --volumelabel=label Give specified volume label (affects ID). 
229 Only meaningful when making image out of directory tree. Default 
230 is no volume label.
232 --timestamp=YYYYMMDDHHMMSS Give specified timestamp for files 
233 (affects ID). Only meaningful when making image out of directory 
234 tree. The default timestamp is 19900101T000000Z.
236 3.4 Image information
238 When invoked as:
240 $ java ImageMaker <imagefile>
242 Variety of information about image is displayed (especially for 
243 floppies/HDDs). Two important fields are calculated and claimed 
244 disk ID. They should be the same. If they are not, then the image 
245 file is corrupt (sadly, imagemaker has bugs and bugs that cause 
246 it to write corrupt images have been seen).
248 3.5 Advanced: The disk ID algorithm
250 The disk ID is calculated as:
252 Skein-256-128-deprecated(<typecode>|<geometry>|<image>)
254 Where Skein-256-128-deprecated is Skein hash function with 
255 256-bit internal state and 128-bit output using the deprecated 
256 rotation constants (as specified in Skein hash function reference 
257 documentation versions 1.0 and 1.1). The <image> is the whole 
258 image, including parts not stored in image file. The reason for 
259 keeping using the deprecated constants are:
261 • Changing the constants would change the IDs, which would 
262   invalidate existing images
264 • This is not about cryptographic security
266 • The new constants don't improve security that much anyway.
268 3.5.1 Floppies and HDDs
270 Floppies have <typecode> value 0 (single byte) and HDDs have 1 
271 (single byte). <geometry> is as follows (this is exactly the same 
272 form as it appears in image header):
274 Byte 0 bits 0-1: Bits 8-9 of track count per side - 1.
276 Byte 0 bits 2-5: Head count - 1.
278 Byte 0 bits 6-7: Reserved, must be 0.
280 Byte 1: Bits 0-7 of track count per side - 1.
282 Byte 2: Sector count per track - 1.
284 3.5.2 CD-ROM and BIOS images
286 CD-ROMs have <typecode> value 2 (single byte) and BIOS images 
287 have 3 (single byte). <geometry> is blank.
289 3.6 Advanced: Disk Image format
291 The disk image consists of following parts:
293 • Magic
295 • Disk ID
297 • Type code
299 • Disk name length
301 • Disk name
303 • type-specific geometry/size data
305 • Actual image data
307 3.6.1 Magic
309 Magic in disk image files is following 5 bytes: “IMAGE”
311 3.6.2 Disk ID
313 Disk ID is given as 16 bytes, encoding the 128-bit disk ID.
315 3.6.3 Type code
317 Type code is single byte. 0 for floppies, 1 for HDDs, 2 for 
318 CD-ROMs and 3 for BIOS images. Other values are reserved.
320 3.6.4 Comment length
322 Disk comment length is given as two-byte big-endian value. New 
323 images should have 0 here.
325 3.6.5 Disk coment
327 Ignored. Comment field is there for backward compatiblity. 
328 Comment length gives length of this field in bytes.
330 3.6.6 Type-specific geometry/size data (floppies and HDDs)
332 Floppies and HDDs have 3-byte geometry data:
334 Byte 0 bits 0-1: Bits 8-9 of track count per side - 1.
336 Byte 0 bits 2-5: Head count - 1.
338 Byte 0 bits 6-7: Reserved, must be 0.
340 Byte 1: Bits 0-7 of track count per side - 1.
342 Byte 2: Sector count per track - 1.
344 3.6.7 Type specific-geometry/size data (CD-ROMs)
346 CD-ROMs have 4-byte big-endian sector (512 bytes!) count.
348 3.6.8 Type specific-geometry/size data (BIOS images)
350 BIOS images have 4-byte big-endian byte (not sector or block) 
351 count.
353 3.6.9 Actual image data (floppy/HDD)
355 Floppy or HDD imagedata consists of following subparts:
357 • Storage method
359 • Sectors present
361 • Image data header
363 • Image data
365 Storage method is single byte. Sectors present gives number of 
366 last nonzero sector + 1 (zero if image is all zeroes)
368 3.6.10 Floppy/HDD storage method 0: Raw storage
370 This storage method has empty header. Image data is raw dump of 
371 first sectors present sectors.
373 3.6.11 Floppy/HDD storage method 1: Sectormap
375 Image data header contains bitfield with just enough bytes to 
376 have one bit per present sector. The order of bits is such that 
377 number of bit corresponding to each sector in byte is sector 
378 number modulo 8 and byte number is floor of sector number divided 
379 by 8 when sector numbers are counted from zero. If bit 
380 corresponding to sector is set, then the sector is present in 
381 image data, otherwise it is absent and assumed to be all-zeroes.
383 Image data contains dumps of all present sectors in order of 
384 increasing sector number.
386 3.6.12 Floppy/HDD storage method 2: Extent first sector zero
388 Image data is empty as storage-specific data is mangled with 
389 image data. The image data alternates between blocks encoding 
390 zero sectors and blocks encoding nonzero sectors. The first block 
391 encodes zero sectors. 
393 Block encoding zero sectors consist of single 1-4 byte 
394 little-endian value encoding number of sectors in block - 1. 
395 Number of bytes is determined by sectors present value. It is 1 
396 for 1-256 sectors, 2 for 257-65536, 3 for 65537-16777216 and 4 
397 for more than 16777216. All sectors in block are filled with 
398 zeroes and are not stored.
400 Block encoding nonzero sectors has same block count as zero 
401 sector block but is followed by the sectors stored raw.
403 3.6.13 Floppy/HDD storage method 3: Extent first sector nonzero
405 Same as storage method 2 but first block is nonzero sector block.
407 3.6.14 Actual image data (CD-ROMs and BIOS images)
409 These store image data raw. The amount of data is specified by 
410 sector/byte count.
412 4 Utilities
414 4.1 org.jpc.utils.RAWToPNG
416 Invoked as:
418 $ java org.jpc.utils.RAWToPNG <input> <outputprefix>
420 Reads RAW video data from <input> (may be named pipe) and dumps 
421 PNG frames received as '<outputprefix><runningcount>.png'. Also 
422 saves '<outputprefix>.timing' which contains frame timing data 
423 (each line consists of time in nanoseconds, space, and filename).
425 5 The actual emulator
427 The actual emulator is invoked as:
429 $ java JPCApplication <options>...
431 The valid options are:
433 -library <library> Use the specified directory when searching for 
434 images (can only be specified once).
436 -autoexec <script> Execute contents of specified file as commands 
437 when starting up.
439 5.1 Command line
441 When emulator is started, command line comes up. Following 
442 commands are known:
444 • 'exit': exit immediately
446 • 'load <plugin>': Load plugin (no arguments)
448 • 'load <plugin>(<arguments>)': load plugin with arguments.
450 • 'command <command> [<arguments>...]': Invoke command via 
451   external command interface.
453 When one gets command line, its useful to load some plugins. See 
454 section about plugins. Note: Load runner plugin 
455 (PCControl/PCRunner and so) last, as some runners like to start 
456 PC immediately.
458 5.2 PC settings dialog notes
460 • CPU divider base frequency before division is 1GHz.
462 • Images can be specified by name or by ID. Name is relative to 
463   library directory. If the image is in subdirectory of image 
464   directory, the directory separator is is '/' regardless of what 
465   the host OS uses.
467 • CD-ROM and hdc are mutually exclusive
469 • Modules is comma-seperated list of modules to load. To pass 
470   arguments to some modules, enclose the arguments in (). Same 
471   module can be specified twice only if parameters differ.
473 • FPU emulator is specified by class name. If core has built-in 
474   FPU emulator, then this should be left blank. Without 
475   core-builtin FPU emulator, blank value means “no fpu”.
477 • Setting boot device doesn't work with some BIOS versions. Those 
478   versions prompt the boot device anyway.
480 5.3 Audio output channels
482 PC can have one or more audio output channels. The name of audio 
483 output associated with PC speaker is: 
484 'org.jpc.emulator.peripheral.PCSpeaker-0'. Modules that have 
485 audio outputs get channel names of form <classname>-<sequential>, 
486 where <classname> is name of main module class and sequential is 
487 number starting from zero. Note that same module can have 
488 multiple output channels. If multiple modules of same class 
489 request audio outputs, the <sequential> values of subsequent 
490 module start where previous left off.
492 5.4 Plugins
494 Plugins actually execute the tasks of the emulator. They can be 
495 loaded using “load <plugin>” or 'load <plugin>(<arguments>)” from 
496 command line.
498 Different Plugins using the same output (like running PCMonitor 
499 and PNGDumper) should not conflict because connector output hold 
500 locking is desinged to handle multiple readers.
502 If no plugin used requires GUI, then the emulator can be run 
503 without having GUI available.
505 5.4.1 plugin: org.jpc.plugins.PCControl
507 No arguments, requires and uses GUI.
509 Runs the PC emulator core. Has capability to start/stop 
510 emulation, breakpoint after certain time or start/end of VGA 
511 vertical retrace. Also can create, savestate and loadstate PC 
512 emulation. Memory dumping is supported. 
514 5.4.2 plugin: org.jpc.plugins.PCRunner
516 Takes 'movie=<file>' as argument and optionally 'stoptime=<time>' 
517 Does not require nor use GUI.
519 Loads PC from savestate and just runs it. CTRL+C to quit. Also 
520 automatically quits once stoptime is reached.
522 5.4.3 plugin: org.jpc.plugins.PCMonitor
524 No arguments, requires and uses GUI.
526 VGA monitor for emulated PC.
528 5.4.4 plugin: org.jpc.plugins.VirtualKeyboard
530 No arguments, requires and uses GUI.
532 On-screen keyboard for emulated PC.
534 5.4.5 plugin: org.jpc.plugins.PCStartStopTest
536 No arguments, requires and uses GUI.
538 Small plugin testing remote PC start/stop. Also supports sending 
539 some common keypresses.
541 5.4.6 plugin: org.jpc.plugins.RAWVideoDumper
543 Takes 'rawoutput=<file>' as argument. Does not require nor use 
544 GUI.
546 Dumps all generated frames to RAW file <file>. Rawoutput is 
547 required. The raw file consists of concatenation of zlib streams. 
548 The uncompressed stream is concatenation of time skips (FFh FFh 
549 FFh FFh), each acting as time offset of 2^32-1 nanoseconds and 
550 saved frames. The saved frame has time offset in nanoseconds (big 
551 endian) as first four bytes (must be at most 2^32-2, as 2^32-1 is 
552 reserved for time skip). The next two bytes are big-endian width, 
553 next two big-endian height. Finally frame has 4 * width * height 
554 bytes of data that encodes pixels using 4 bytes per pixel, in 
555 left-to-right, up-to-down order. Byte 0 of each pixel is 
556 reserved, byte 1 is the red channel, byte 2 is green channel and 
557 byte 3 is blue channel.
559 Dumping to pipe is supported.
561 5.4.7 plugin: org.jpc.plugins.RAWAudioDumper
563 Takes 'src=<name of audio output channel>', 
564 'file=<output-filename>' and 'offset=<offset>' as arguments, 
565 separated by ','. Does not require nor use GUI.
567 Dumps output from specified audio output channel (src, mandatory) 
568 to RAW-format file (file, mandatory). The resulting file consists 
569 of records, 8 bytes each. Each record has three fields. First 4 
570 byte unsinged little endian timedelta value (in nanoseconds), 
571 then 2 byte unsigned little endian new left channel volume, then 
572 2 byte unsigned little endian new right channel volume. 
573 Optionally 'offset' can be set to positive value (in nanoseconds) 
574 to delay the audio by.
576 5.4.8 plugin: org.jpc.plugins.LuaPlugin
578 Takes 'kernel=<name of lua kernel file>', other parameters are 
579 passed to kernel, requires and uses GUI.
581 Lua VM for executing scripts.
583 6 Some error messages and explanations
585 • <filename> is Not a valid image file
587 • <filename> is not image file
589 • <filename> claims to be floppy with illegal geometry: <x> 
590   tracks, <y> sides and <z> sectors.
592 • <filename> claims to be HDD with illegal geometry: <x> tracks, 
593   <y> sides and <z> sectors.
595 • Can't read disk image sector map.
597 • Can't read disk image extent.
599 Code expects <filename> to be valid JPC-RR format image, but it 
600 isn't JPC-RR image at all or its corrupt.
602 • <filename> is image of unknown type.
604 • <filename> has unrecognized geometry <x> <y> <z>
606 Possibly corrupt image, not JPC-RR image, or JPC-RR image from 
607 future version containing something current version can't 
608 comprehend.
610 • Invalid format specifier <something>.
612 • Invalid syntax of --floppy= or --HDD= option.
614 • Invalid format specifier/option <something>.
616 Invalid option or format specifier was given. Check for typos.
618 • java ImageMaker [<options>...] <format> <destination> <source> 
619   <diskname>
621 Check syntax of command. Especially that diskname is present!
623 • The image has <nnn> sectors while it should have <yyy> 
624   according to selected geometry.
626 • Raw image file length not divisible by 512.
628 • Trying to read sector out of range.
630 The selected geometry is wrong or raw image is incomplete.
632 • Invalid disk name (Should not happen!).
634 • Invalid geometry to be written.
636 This is a very likely a bug in program.
638 • What the heck <filename> is? It's not regular file nor 
639   directory.
641 That sort of file can't be used as input for image making, or the 
642 file just doesn't exist.
644 • BIOS images can only be made out of regular files.
646 • CD images can only be made out of regular files.
648 Source image specified is not regular file, but image of that 
649 type can't be made of anything else.
651 • Can't read raw bios image file.
653 • Can't read sector <nnn> from image.
655 Reading the raw image file failed for some reason.
657 • Bad library line: "<something>". Ignored.
659 Syntax error in image library.
661 • Removing image <something> a.k.a. "<something>" as it no longer 
662   exists.
664 The image file no longer exists so it gets removed from library.
666 • Removing image <something> a.k.a. "<something>" due to <some> 
667   conflict.
669 Image library code killed some image from library due to some 
670 kind of conflict with image being added.
672 • Too much data to fit into given space.
674 The tree you gave contains takes just too much space to fit into 
675 disk of this size.
677 7 Advanced: Savestate/movie format
679 7.1 JRSR archive
681 JRSR archive format packs multiple text archive members to text 
682 archive. It does not support binary members. JRSR archives have 
683 first five bytes form the magic. It is “JRSR” followed by 
684 LINEFEED character (0x0A, 0x0D, 0x0D 0x0A, 0xC2 0x85). There are 
685 three kinds of lines after that (lines are terminated by LINEFEED 
686 character):
688 • Start member
690 • Member line
692 • End member
694 Sequencing rules are as follows: Start member is allowed 
695 anywhere. Member line is allowed only inside member (member 
696 started but not ended). End member is only allowed inside member. 
697 End of file is only allowed outside member. Note that those three 
698 are all the kinds of lines allowed.
700 7.1.1 Start member
702 Start member line is given as “!BEGIN” <SPACE> <membername> 
703 <LINEFEED>. <SPACE> is SPACE character (only one!, 0x20) and 
704 <LINEFEED> is LINEFEED chacter. Multiple <SPACE>s would mean 
705 member with name beginning with <SPACE>. The member name is UTF-8 
706 encoded and maximum allowed length is 1024 bytes (256-1024 
707 codepoints). Starting member inside another implicitly ends the 
708 previous member.
710 7.1.2 Member line:
712 Member line is given as “+”<content><LINEFEED>. It gives another 
713 line for member contents. <content> is passed raw to layers 
714 above.
716 7.1.3 End member
718 End member line is given as “!END”<LINEFEED>. It ends the current 
719 member. The following line can only be start member line or file 
720 may end.
722 7.2 Four-to-Five encoding
724 Binary members are encoded into text by so-called four-to-five 
725 encoding. This encoding can encode single byte to two, two bytes 
726 to three, three bytes to four and four bytes to five. 
727 Four-to-five encoding has five kinds of blocks. All SPACE and 
728 CHARACTER TABULATION characters are completely ignored, even in 
729 middle of blocks.
731 7.2.1 End stream block
733 End stream block is encoded as '!'. It ends the stream instantly. 
734 There is also implicit end of stream at end of input to decoding.
736 7.2.2 Other four block types
738 Other four block types take the value to be encoded, read it as 
739 big-endian value. Then they write it as base-93 big-endian value. 
740 Then length specific constants are added to digits of that number 
741 to yield ASCII values for characters (those are stored in order):
744 +------------+------------+------------+------------+------------+-----------+
745 | To encode  | 1st char.  | 2nd char.  | 3rd char.  | 4th char.  | 5th char. |
746 +------------+------------+------------+------------+------------+-----------+
747 +------------+------------+------------+------------+------------+-----------+
748 |  1 byte    |    34      |    34      |     -      |     -      |     -     |
749 +------------+------------+------------+------------+------------+-----------+
750 |  2 bytes   |    37      |    34      |    34      |     -      |     -     |
751 +------------+------------+------------+------------+------------+-----------+
752 |  3 bytes   |    45      |    34      |    34      |    34      |     -     |
753 +------------+------------+------------+------------+------------+-----------+
754 |  4 bytes   |    66      |    34      |    34      |    34      |    34     |
755 +------------+------------+------------+------------+------------+-----------+
758 Blocks which encode values greater than what is possible for 
759 value of that length are fatal errors. 
761 7.3 UTF-8 encoding
763 UTF-8 encoding is used to encode lines of Unicode codepoints into 
764 bytes.
766 7.4 Line component encoing
768 Line component encoding sits on top of UTF-8 encoding. Line 
769 component encoding encodes non-empty 1-D array of non-empty 
770 strings into line, and thus array of those into member. Empty 
771 lines or lines that don't contain any components are ignored. 
772 Line starts with depth value of 0 and must end with depth value 
773 of zero.
775 Components are seperated by component separators. Empty 
776 components are ignored. Following codepoints are separators on 
777 depth 0 if not escaped:
779 • Codepoint of '('. The depth is read pre-increment.
781 • Codepoint of ')'. The depth is read post-decrement.
783 • Codepoints 0x20, and 0x09.
785 • Codepoints 0x1680, 0x180E, 0x2028, 0x205F and 0x3000
787 • Codepoints 0x2000-0x200A.
789 The following characters are special:
791 • '('. Increments depth by 1 if not escaped (and appears in 
792   component).
794 • ')'. Decrements depth by 1 if not escaped (and appears in 
795   component). Depth going negative is an error.
797 • '\'. Next character is interpretted as literal. Error if at end 
798   of line.
800 Otherwise, characters are interpretted as literals and appear in 
801 components. Depth must be zero at end of line.
803 7.5 Header section:
805 Header section is in archive member "header". It uses line 
806 component encoding. The first component of each line is name of 
807 header, and subsequent ones are arguments. How many parameters 
808 are expected is dependent on what header it is:
810 7.5.1 PROJECTID header:
812 • Header name: "PROJECTID"
814 • Components: 2
816 • Argument #1: <project-id-string>
818 • Mandatory: Yes
820 Gives project ID. Project ID is generated when PC is assembled 
821 and is then preserved in save states. It is used for computing 
822 rerecord counts. Emulator treats it as opaque string, the IDs it 
823 generates are formed by 48 random hexadecimal digits.
825 7.5.2 SAVESTATEID header:
827 • Header name: "SAVESTATEID"
829 • Components: 2
831 • Argument #1: <savestate-id-string>
833 • Mandatory: No
835 Gives save state ID. Each save state has its own save state ID. 
836 Treated as opaque string, but generated as 48 random hexadecimal 
837 digits. The presence of this header signals whether there is save 
838 state to be loaded. If this header is present, save state load 
839 will be attempted. If absent, save state is not to be loaded even 
840 if present (and correct savestate load would be technically 
841 impossible anyway).
843 The value is used to prevent loading incompatible save states in 
844 preserve event stream mode and also to find the point in event 
845 stream where one left off.
847 7.5.3 RERECORDS header:
849 • Header name: "RERECORDS"
851 • Components: 2
853 • Argument #1: <rerecords>
855 • Mandatory: Yes
857 Gives rerecord count. PC assembly (except when loading save 
858 state) initializes current rerecord count to zero. Must be 
859 non-negative and decimal number using ASCII digit characters.
861 On loading save state:
863 1) If project ID matches with previous:
865 1a) If loaded rerecord count is larger or equal to current 
866 rerecord count:
868 1a-a) Current rerecord count is loaded rerecord count + 1.
870 1b) Otherwise
872 1b-a) Current rerecord count increments by 1.
874 2) Otherwise
876 2a) Current rerecord count is loaded rerecord count + 1.
878 The current rerecord count at time of save is saved to save 
879 state.
881 7.5.4 AUTHORS header:
883 • Header name: "AUTHORS"
885 • Components: 2 or more
887 • Arguments: free form
889 • Mandatory: No
891 Gives authors of run. Each argument gives one author. May be 
892 present multiple times.
894 7.5.5 COMMENT header:
896 • Header name: "COMMENT"
898 • Components: 2 or more
900 • Arguments: free form
902 • Mandatory: No
904 Various kinds of free form data. Not parsed further by emulator.
906 7.6 Initialization segment:
908 If SAVESTATEID header isn't present (not a save state), member 
909 "initialization" gives PC initialization parameters for 
910 assembling the PC. It is present anyway even if SAVESTATEID is 
911 present (savestate).
913 Following parameters are used (space separates components):
915 "BIOS" <id>
917 Gives Image ID of main system BIOS (mandatory)
919 "VGABIOS" <id>
921 Gives Image ID of VGA BIOS (mandatory).
923 "HDA" <id>
925 Gives Image ID of hda. Present only if system has hard disk hda.
927 "HDB" <id>
929 Gives Image ID of hdb. Present only if system has hard disk hdb.
931 "HDC" <id>
933 Gives Image ID of hdc. Present only if system has hard disk hdc.
935 "HDD" <id>
937 Gives Image ID of hdd. Present only if system has hard disk hdd.
939 "DISK" <num> <id>
941 Gives Image ID of disk in slot <num>. Slot number must be 
942 non-negative.
944 “DISKNAME” <num> <name>
946 Gives image name of disk in slot <num>. Slot number must be 
947 non-negative. The slot must be previously declared using “DISK”.
949 "FDA" <num>
951 Gives Image slot to initially put into floppy drive fda. Disk 
952 must be of floppy type. If none present, no disk is initially put 
953 there.
955 "FDB" <num>
957 Gives Image slot to initially put into floppy drive fdb. Disk 
958 must be of floppy type. If none present, no disk is initially put 
959 there.
961 "CDROM" <num>
963 Gives Image slot to initially put into CD-ROM drive hdc. Not 
964 allowed if hard disk hdc is present. Disk must be of CD-ROM type. 
965 If none present no disk is initially put there.
967 "INITIALTIME" <time>
969 Number of milliseconds since Unix epoch to system start up time. 
970 Allowed range:
972 0-4102444799999. Mandatory.
974 "CPUDIVIDER" <divider>
976 Set CPU frequency divider (dividing the 1GHz master clock). 
977 Allowed range is 1-256. Mandatory.
979 "MEMORYSIZE" <pages>
981 Number of 4KiB pages of RAM memory. Allowed range 256-262144. 
982 Mandatory.
984 "BOOT" <device>
986 Set boot device. Valid devices are "FLOPPY" (boot from fda), 
987 "HDD" (boot from hda) and "CDROM" (boot from CD).
989 "LOADMODULEA" <module> <parameters>
991 Load module <module> with parameters <parameters>.
993 "LOADMODULE" <module>
995 Load module <module> with no parameters
997 “FPU” <fpu>
999 Use class <fpu> as FPU emulator.
1001 7.7 Event record format:
1003 Event record is in archive member "events". It uses line 
1004 component encoding. Each line gives an event. First component of 
1005 each line gives time stamp. These timestamps MUST be in 
1006 increasing order and all MUST be non-negative. Time stamp time 
1007 unit is exactly 1 nanosecond of emulated time.
1009 The second component of each line is name of class to dispatch 
1010 to. Further components are passed as-is to event handlers. 
1011 "Class" name "SAVESTATE" is special. This takes one or two 
1012 additional components, first of which gives the save state ID of 
1013 save state that occurred there. The save state IDs MUST be unique 
1014 in entire event stream. The second argument to savestate (if 
1015 present) is rerecord count at time of saving that savestate 
1016 (useful for calulating rerecord count of movie starting from 
1017 savestate).
1019 7.7.1 Keyboard keypress/keyrelease event:
1021 • Dispatch to: org.jpc.emulator.peripheral.Keyboard
1023 • Argument #1: Fixed: "KEYEDGE"
1025 • Argument #2: Key number. Valid values are 1-83, 85-95, 129-197 
1026   and 199-223
1028 Send key press or key release. Keys work in toggle button manner. 
1029 The event time must be multiple of 66 666, and must not be less 
1030 than 60 * 66 666 TUs after last PAUSE event, 20 * 66 666 TUs 
1031 after last KEYEDGE on key >128 and 10 * 66 666 TUs after last 
1032 KEYEDGE on key <128.
1034 7.7.2 Pause event:
1036 • Dispatch to: org.jpc.emulator.peripheral.Keyboard
1038 • Argument #1: Fixed: "PAUSE"
1040 Send pause key event. The time restrictions are identical to 
1041 KEYEDGE event.
1043 7.7.3 Reboot:
1045 • Dispatch to: org.jpc.emulator.PC$ResetButton
1047 • No arguments
1049 Reboots the PC.
1051 7.7.4 Fda disk change:
1053 • Dispatch to: org.jpc.emulator.PC$DiskChanger
1055 • Argument #1: Fixed: "FDA"
1057 • Argument #2: Number of image slot to put there. 
1059 The disk number MUST be -1 or valid disk number. -1 MUST NOT be 
1060 used if there is no disk in floppy drive A. This event causes 
1061 specified disk to be placed to FDA or FDA disk to be ejected with 
1062 no replacement if disk number is -1. The specified disk if not -1 
1063 must be of floppy type. The specified disk if valid must not be 
1064 in any other drive.
1066 7.7.5 Fdb disk change:
1068 • Dispatch to: org.jpc.emulator.PC$DiskChanger
1070 • Argument #1: Fixed: "FDB"
1072 • Argument #2: Number of image slot to put there. 
1074 The disk number MUST be -1 or valid disk number. -1 MUST NOT be 
1075 used if there is no disk in floppy drive B. This event causes 
1076 specified disk to be placed to FDB or FDB disk to be ejected with 
1077 no replacement if disk number is -1. The specified disk if not -1 
1078 must be of floppy type. The specified disk if valid must not be 
1079 in any other drive.
1081 7.7.6 Change CDROM:
1083 • Dispatch to: org.jpc.emulator.PC$DiskChanger
1085 • Argument #1: Fixed: "CDROM"
1087 • Argument #2: Number of image slot to put there. 
1089 The disk number MUST be -1 or valid disk number. -1 MUST NOT be 
1090 used if there is no disk in CD-ROM. This event causes specified 
1091 disk to be placed to CD-ROM or CD-ROM disk to be ejected with no 
1092 replacement if disk number is -1. The specified disk if not -1 
1093 must be of CD-ROM type.
1095 This event has no effect if CD-ROM is locked.
1097 7.7.7 Write protect floppy:
1099 • Dispatch to: org.jpc.emulator.PC$DiskChanger
1101 • Argument #1: Fixed: "WRITEPROTECT"
1103 • Argument #2: Number of image slot to manipulate 
1105 Write protects specified disk. The disk MUST NOT be in any drive 
1106 and MUST be valid floppy-type disk.
1108 7.7.8 Write unprotect floppy:
1110 • Dispatch to: org.jpc.emulator.PC$DiskChanger
1112 • Argument #1: Fixed: "WRITEUNPROTECT"
1114 • Argument #2: Number of image slot to manipulate 
1116 Disables write protection specified disk. The disk MUST NOT be in 
1117 any drive and MUST be valid floppy-type disk.
1119 7.8 Savestates
1121 Actual savestate format is not documented here. It is close to 
1122 impossible to comprehend without access to emulator source 
1123 anyway.
1125 8 Advanced: Making class dumpable
1127 Class is made dumpable by implementing interface 
1128 org.jpc.emulator.SRDumpable and implementing method 
1129 dumpSRPartial(org.jpc.emulator.SRDumper) and constructor 
1130 <init>(org.jpc.emulator.SRLoader). Non-static inner classes can 
1131 not be dumpable (make them static using tricks similar to what 
1132 javac uses).
1134 If dumped class has dumpable superclass, the first thing dumping 
1135 function needs to do is to call dumper function of superclass and 
1136 first thing loading constructor needs to do is to call loading 
1137 constructor of superclass. If class has no dumpable superclass, 
1138 dumper doesn't need to do anything special, while loader needs to 
1139 call objectCreated(this) on SRLoader object passed as parameter. 
1141 Following these fixed parts, dump all members that are part of 
1142 mutable state in emulator core.
1144 8.1 Member dumping/loading functions
1146 There is dumping/loading function for following (all functions 
1147 dumping/loading reference types can handle null):
1149 • boolean: SRDumper.dumpBoolean, SRLoader.loadBoolean
1151 • byte: SRDumper.dumpByte, SRLoader.loadByte
1153 • short: SRDumper.dumpShort, SRLoader.loadShort
1155 • int: SRDumper.dumpInt, SRLoader.loadInt
1157 • long: SRDumper.dumpLong, SRLoader.loadLong
1159 • String: SRDumper.dumpString, SRLoader.loadString
1161 • boolean[]: SRDumper.dumpArray, SRLoader.loadArrayBoolean
1163 • byte[]: SRDumper.dumpArray, SRLoader.loadArrayByte
1165 • short[]: SRDumper.dumpArray, SRLoader.loadArrayShort
1167 • int[]: SRDumper.dumpArray, SRLoader.loadArrayInt
1169 • long[]: SRDumper.dumpArray, SRLoader.loadArrayLong
1171 • double[]: SRDumper.dumpArray, SRLoader.loadArrayDouble
1173 • <dumpable type>: SRDumper.dumpObject, SRLoader.loadObject
1175 • special object: SRDumper.specialObject, SRLoader.specialObject
1177 8.1.1 Notes:
1179 • Dumpable objects come out as type of 
1180   org.jpc.emulator.SRDumpable.
1182 • Special objects are various static objects that don't need to 
1183   be stored because they don't have mutable fields.
1185 • Don't dump fields related to event state feedback.
1187 • Don't dump temporary flags that are only used while PC is 
1188   running. Savestate when PC is running isn't possible anyway.
1190 • Some connectors dump fields related to connector output, some 
1191   don't.
1193 9 Advanced: Making output connectors
1195 Implementing interface org.jpc.emulator.DisplayController signals 
1196 that this is display controller, inhibiting loading of the 
1197 standard VGA display controller if loaded as module. 
1199 9.1 Interface org.jpc.emulator.OutputConnector
1201 Class is made to be output connector by implementing this 
1202 interface. This interface specifies the methods used for output 
1203 hold locking. Class org.jpc.emulator.OutputConnectorLocking has 
1204 implementations of these that are suitable for calling. 
1206 9.1.1 Method subscribeOutput(Object)
1208 Subscribes the output, with specified object as handle.
1210 9.1.2 Method unsubscribeOutput(Object)
1212 Unsubscribe the specified handle object from output.
1214 9.1.3 Method waitOutput(Object)
1216 Wait for output on specified connector using specified handle 
1217 object. Returns true on success, false if wait was interrupted by 
1218 thread interrupt. Blocking.
1220 9.1.4 Method releaseOutput(Object)
1222 Release connector from p.o.v. of given handle. Does not block.
1224 9.1.5 Method holdOutput()
1226 Release threads waiting on waitOutput() and block until all 
1227 subscribers have returned from waitOutput() and enteired 
1228 releaseOutput().
1230 9.1.6 Method releaseOutputWaitAll(object)
1232 Like releaseOutput(), but waits until all handles have released 
1233 their output.
1235 9.2 Class org.jpc.emulator.VGADigtalOut
1237 Class org.jpc.emulator.VGADigtalOut (already implements 
1238 OutputConnector) implements VGA output connector. If module 
1239 provodes output connector, it needs to implement 
1240 org.jpc.emulator.DisplayController.
1242 9.2.1 Method getWidth()
1244 Get width of display (watch out, can return 0).
1246 9.2.2 Method getHeight()
1248 Get height of display (watch out, can return 0).
1250 9.2.3 Methods getDirtyXMin(), getDirtyXMax(), getDirtyYMin(), 
1251   getDirtyYMax()
1253 Returns the dirty region (region modified since last output).
1255 9.2.4 Method getBuffer()
1257 Get buffer of ints, at least width * height elements 
1258 (left-to-right, top-down, one value per pixel) giving pixel data. 
1259 Value for each pixel is 65536 * <red-component> + 256 * 
1260 <green-component> + <blue-component>.
1262 9.2.5 Method resizeDisplay(int _width, int _height)
1264 Resize the display to be of specified size.
1266 9.2.6 Method dirtyDisplayRegion(int x, int y, int w, int h)
1268 Mark the specified region as dirty.
1270 9.2.7 Method resetDirtyRegion()
1272 Resets the dirty region to be empty.
1274 9.3 Class org.jpc.emulator.PC method getVideoOutput()
1276 Get VGA output connector for PC.
1278 9.4 Interface org.jpc.emulator.DisplayController.
1280 Implementing this class signals that module is VGA controller. 
1281 There can be only one such module active at time and presence of 
1282 such module prevents loading builtin VGA controller emulation 
1283 code.
1285 9.4.1 Method getOutputDevice()
1287 Get VGA output connector for this VGA device.
1289 9.5 Class org.jpc.emulator.SoundDigitalOut
1291 Class org.jpc.emulator.SoundDigitalOut provodes output connector 
1292 for sound. Each connector can transfer stereo signal at arbitiary 
1293 sampling rate. Modules that have audio connectors need to 
1294 implement interface org.jpc.emulator.SoundOutputDevice, as this 
1295 signals that output connectors should be created.
1297 9.5.1 Method addSample(long, short, short)
1299 Add stereo sample at time given by first argument. The second and 
1300 third arguments give volume on left and right channels.
1302 9.5.2 Method addSample(long, short)
1304 Add mono sample at time given by first argument. The second 
1305 argument give volume on both channels.
1307 9.5.3 Method readBlock(Block)
1309 Reads block of output (atomic versus addSample). Block structure 
1310 has following fields which are filled:
1312 • timeBase: Time base for block.
1314 • baseLeft: Left volume at time base.
1316 • baseRight: Right volume at time base
1318 • blockNo: Sequence number of block filled.
1320 • samples: Number of samples in block
1322 • sampleTiming: Number of nanoseconds since last sample
1324 • sampleLeft: Left channel samples
1326 • sampleRight: Right channel samples
1328 9.6 Interface org.jpc.emulator.SoundOutputDevice
1330 Implementing this interface signals that module has audio output 
1331 channels.
1333 9.6.1 Method 
1334   org.jpc.emulator.SoundOutputDevice.requestedSoundChannels()
1336 Return the number of sound channels module has.
1338 9.6.2 Method 
1339   org.jpc.emulator.SoundOutputDevice.soundChannelCallback(SoundDigitalOut)
1341 This is called once per sound channel requested giving precreated 
1342 sound channel.
1344 9.7 Class org.jpc.emulator.PC method getSoundOut(String)
1346 Get sound output with specified name.
1348 10 Advanced: Writing event targets
1350 Whereas output connectors are the way output is dispatched, input 
1351 is dispatched via event targets. Event targets need to implement 
1352 interface org.jpc.emulator.EventDispatchTarget.
1354 Event targets also provode methods which then encode events and 
1355 dispatch them forward (without doing anything else) to event 
1356 recorder. Also, event targets may have methods for obtaining 
1357 state.
1359 10.1 Interface org.jpc.emulator.EventDispatchTarget
1361 Interface that marks class capable of receiving events.
1363 10.1.1 Method setEventRecorder(EventRecorder)
1365 Set the event recorder input events are sent to.
1367 10.1.2 Method startEventCheck()
1369 Signals target to reset all state related to event checking and 
1370 state feedback. This may be called at any time in order to 
1371 reinitialialize event checking/feedback state.
1373 10.1.3 Method doEvent(long, String[], int) throws IOException
1375 Event dispatch handler. The first argument is event time, second 
1376 is parameters and third is what to do with it. If target doesn't 
1377 like the event, throw IOException. Following types (the integer 
1378 parameter) are used:
1380 0 (EventRecorder.EVENT_TIMED): Time has been assigned for event.
1382 1 (EventRecorder.EVENT_STATE_EFFECT_FUTURE): Future event in 
1383 event replay for reinitialization
1385 2 (EventRecorder.EVENT_STATE_EFFECT): Past event in event replay 
1386 reinitialization
1388 3 (EventRecorder.EVENT_EXECUTE): This event occurs now. Execute 
1389 the effect.
1391 10.1.4 Method endEventCheock()
1393 End event reinitialization. Usually unused.
1395 10.1.5 Method getEventTimeLowBound(long, String[]) throws 
1396   IOException
1398 Return the time value that's the earliest possiblity for this 
1399 event to occur. Returning any time in past (including -1) causes 
1400 event to fire as soon as possible. The long parameter gives the 
1401 current scheduled time for event.
1403 11 Writing modules
1405 Modules are various extensions that run inside emulator core. As 
1406 such, they affect sync. Modules must implement interface 
1407 org.jpc.emulator.HardwareComponent (they are hardware components) 
1408 and must be dumpable. Additionally, they need either constructor 
1409 <init>() or <init>(String). The first is if no parameters are 
1410 passed, the second is for case where parameters are passed.
1412 Aside of the constructors, modules need to obey the ordinary 
1413 conventions for hardware components. No code outside modules 
1414 needs to know that module exists.
1416 12 Writing plugins
1418 Plugins handle various UI tasks. They need to implement interface 
1419 org.jpc.Plugin.
1421 12.1 Interface org.jpc.pluginsbase.Plugin
1423 12.1.1 Method systemShutdown()
1425 Called when emulator shuts down. Either called in dedicated 
1426 thread or in thread that called emulatorShutdown(). These 
1427 handlers should do the bare minimum to get files on disk to 
1428 consistent state. After these calls from all plugins have 
1429 finished, emulator exits. Do not try to manipulate UI from these 
1430 methods, as doing that easily leads into deadlock.
1432 12.1.2 Method reconnect(PC) 
1434 Gives new PC to connect to. Null is passed if plugin should 
1435 disconnect.
1437 12.1.3 Method main()
1439 Called in dedicated thread after plugin is initialized.
1441 12.1.4 Method pcStopping()
1443 Called after PC has stopped.
1445 12.1.5 Method pcStarting()
1447 Called before PC starts.
1449 12.1.6 Method notifyArguments(String[])
1451 Pass arguments from command line.
1453 12.1.7 Constructor <init>(Plugins)
1455 This constructor is used to initialize plugins that don't take 
1456 parameters.
1458 12.1.8 Constructor <init>(Plugins, String)
1460 This constructor is used to initialize plugins that take 
1461 parameters.
1463 12.2 Class org.jpc.pluginsbase.Plugins
1465 This class provodes various methods for manipulating plugins.
1467 12.2.1 Method isShuttingDown()
1469 Returns true if Plugins.shutdownEmulator() has been called 
1470 somehow, either via VM exit, CTRL+C or explicitly. Useful to skip 
1471 cleanups involving GUI, as these are too deadlock-prone.
1473 12.2.2 Method shutdownEmulator()
1475 Shut down and exit the emulator. All plugin shutdown functions 
1476 are called in this thread.
1478 12.2.3 Method reconnectPC(PC)
1480 Signal reconnectPC event to all plugins.
1482 12.2.4 Method pcStarted()
1484 Signal pcStarting() event to all plugins.
1486 12.2.5 Method pcStopped()
1488 Signal pcStopping() event to all plugins.
1490 12.3 Interface org.jpc.pluginsbase.ExternalCommandInterface
1492 Implementing interface 
1493 org.jpc.pluginsbase.ExternalCommandInterface signals that plugin 
1494 can receive commands via external commands interface.
1496 12.3.1 Method invokeCommand(String cmd, String[] args)
1498 Invoke specified command using specified arguments. Return true 
1499 if event is to be shallowed, false to continue trying to pass it 
1500 to more plugins.
1502 13 Lua kernel programming
1504 At startup, kernel gets its arguments in 'args' table and the 
1505 script name to run in 'scriptname' string. It should enter the 
1506 named script in protected mode.
1508 The Lua VM exports numerious callbacks to kernel. The kernel can 
1509 then choose to omit, wrap or re-export these to Lua scripts.
1511 • Always grab any functions used into local variables so nobody 
1512   can mess with them
1514 • Don't use global variables in kernel (except for those passed).
1516 13.1 Lua standard
1518 All standard Lua functions in tables main, 'math', 'coroutine', 
1519 'string' and 'table' (safe to re-export with exception of print, 
1520 loadfile and dofile, which should be wrapped).
1522 13.2 Bit manipulation: none
1524 Bitwise none function is exported as 
1525 'jpcrr_raw.bit_none(num...)'. It takes zero or more numbers and 
1526 returns number (numbers have 48 bits). The number returned has 
1527 those bits set that are set in none of inputs. Safe to re-export.
1529 13.3 Bit manipulation: any
1531 Bitwise any function is exported as 'jpcrr_raw.bit_any(num...)'. 
1532 It takes zero or more numbers and returns number (numbers have 48 
1533 bits). The number returned has those bits set that are set in any 
1534 of inputs. Safe to re-export.
1536 13.4 Bit manipulation: parity
1538 Bitwise parity function is exported as 
1539 'jpcrr_raw.bit_parity(num...)'. It takes zero or more numbers and 
1540 returns number (numbers have 48 bits). The number returned has 
1541 those bits set that are set in even number of of inputs. Safe to 
1542 re-export.
1544 13.5 Bit manipulation: all
1546 Bitwise parity function is exported as 
1547 'jpcrr_raw.bit_all(num...)'. It takes zero or more numbers and 
1548 returns number (numbers have 48 bits). The number returned has 
1549 those bits set that are set in all of inputs. Safe to re-export.
1551 13.6 Bit manipulation: lshift
1553 Left shift function is exported as 'jpcrr_raw.bit_lshift(num 
1554 base, num shift)'. It takes two numbers and returns first number 
1555 shifted by second. number places left. Numbers have 48 bits, 
1556 using shifts outside 0-47 gives unpredictable results. Safe to 
1557 re-export.
1559 13.7 Bit manipulation: rshift
1561 Right logical shift function is exported as 
1562 'jpcrr_raw.bit_rshift(num base, num shift)'. It takes two numbers 
1563 and returns first number shifted by second number places right. 
1564 Numbers have 48 bits, using shifts outside 0-47 gives 
1565 unpredictable results. Safe to re-export.
1567 13.8 Bit manipulation: arshift
1569 Right arithmetic shift function is exported as 
1570 'jpcrr_raw.bit_arshift(num base, num shift)'. It takes two 
1571 numbers and returns first number shifted by second number places 
1572 right, with highest bit copied. Numbers have 48 bits, using 
1573 shifts outside 0-47 gives unpredictable results. Safe to 
1574 re-export.
1576 13.9 Bit manipulation: add
1578 Add function is exported as 'jpcrr_raw.bit_add(num...)'. It takes 
1579 zero or more numbers and returns number (numbers have 48 bits). 
1580 The number returned is sum of all of inputs modulo 2^{48}. Safe 
1581 to re-export.
1583 13.10 Bit manipulation: addneg
1585 Add negated function is exported as 
1586 'jpcrr_raw.bit_addneg(num...)'. It takes zero or more numbers and 
1587 returns number (numbers have 48 bits). The number returned is 0 
1588 minus of all of inputs modulo 2^{48}. Safe to re-export.
1590 13.11 Bit manipulation: addalt
1592 Add alternate function is exported as 
1593 'jpcrr_raw.bit_addalt(num...)'. It takes zero or more numbers and 
1594 returns number (numbers have 48 bits). The number returned is sum 
1595 of odd arguments (first argument is odd) minus sum of even 
1596 arguments modulo 2^{48}. Safe to re-export.
1598 13.12 Bit manipulation: tohex
1600 Add alternate function is exported as 'jpcrr_raw.bit_tohex(num 
1601 number)'. It takes number and returns hexadecimal representation 
1602 of it. Safe to re-export.
1604 13.13 Console output:
1606 Console ouput function is exported as 
1607 'jpcrr_raw.print_console_msg(sring line)'. It prints its string 
1608 argument as new line on Lua console (should not be re-exported, 
1609 use to construct replacement print).
1611 13.14 Binary I/O:
1613 Binary I/O file open function is exported as 
1614 'jpcrr_raw.io_openbinary(string name, string mode)'. It takes two 
1615 strings, first is name of file to open, second is mode, either “r”
1616  or “rw”. The returned object has following methods:
1618 • file:length(): Returns length of file as number.
1620 • file:set_length(num len): Truncate file to len bytes.
1622 • file:read(num offset, num length): Read up to length bytes 
1623   starting from offset offset. The file character set is assumed 
1624   to be Latin1.
1626 • file:write(num offset, string str): Write str starting from 
1627   offset offset. Writing non-Latin1 characters gives 
1628   unpredictable results. Only available on files opened for 
1629   read/write.
1631 • file:close(): Close file. No file operations can be done 
1632   anymore.
1634 The function should be filtered to enforce path constraints.
1636 13.15 Text input:
1638 Text input open function is exported as 
1639 'jpcrr_raw.io_opentextinput(string name)'. It takes one string, 
1640 name of file to open. The returned object has following methods:
1642 • file:read(): Read next line. The file encoding is assumed to be 
1643   UTF-8.
1645 • file:close(): Close file. No file operations can be done 
1646   anymore.
1648 The function should be filtered to enforce path constraints.
1650 13.16 Text output:
1652 Text output open function is exported as 
1653 'jpcrr_raw.io_opentextoutput(string name)'. It takes one string, 
1654 name of file to open. The returned object has following methods:
1656 • file:write(string line): Write line line to file. File encoding 
1657   is assumed to be UTF-8.
1659 • file:close(): Close file. No file operations can be done 
1660   anymore.
1662 The function should be filtered to enforce path constraints.
1664 13.17 ECI invoke:
1666 ECI invocation functions 'jpcrr_raw.invoke(string name [, table 
1667 args])' and 'jpcrr_raw.invoke_synchronous(string name [, table 
1668 args])'. These functions run specified ECI function with 
1669 specified arguments. The updates ECI call makes to arguments is 
1670 reflected to table passed as parameter. The synchronous version 
1671 waits that any slow command processing gets done before returning 
1672 (assemble, save, load and dump have such slow processing). These 
1673 functions return nothing. Safe to re-export, use as base for more 
1674 functions.
1676 13.18 PC Running:
1678 'jpcrr_raw.pc_running()'. Returns true if PC is running, false 
1679 otherwise. Assemble, save, load and dump require PC not being 
1680 running to be possible.
1682 13.19 PC Connected:
1684 'jpcrr_raw.pc_connected()'. Returns true if PC is connected, 
1685 false otherwise.
1687 13.20 VGA wait/release:
1689 'jpcrr_raw.wait_vga()' waits for VGA to enter frame hold mode and 
1690 returns true (if it returns false, the wait got interrupted for 
1691 some reason and VGA is in rendering mode). While VGA is in frame 
1692 hold, PC execution is locked out and things like memory reading 
1693 and writing can be done without races (but load/save is not 
1694 possible). 'jpcrr_raw.release_vga()' acknowledges frame hold 
1695 processing (VGA can re-enter rendering mode).
1697 Frame hold happens once per frame at start of vertical retrace. 
1698 Note that running Lua script must wait frame holds and release 
1699 them or main PC execution will hang on first frame hold.