Browse by type
s68k is an M68K assembler and interpreter written in Rust. The assembler reports errors and warnings to help with learning assembly and encodes instructions in memory as EASy68K does. The interpreter runs the assembled program and supports stepping, undo, and breakpoints.
The library compiles to WebAssembly for use from JavaScript and Node.js. It is available on npm.
It is part of a family of JavaScript assembly interpreters and simulators:
s68k is a teaching tool for M68K assembly. Its diagnostics explain invalid syntax, addressing modes, and directives. It can write programs as S-record files in the format produced by EASy68K's assembler and loaded by its simulator. It is not intended to produce binaries for real 68000 systems.
The library has four parts:
Assembler (src/assembler/): reads project source files starting from the entry file. It reports all diagnostics it finds and, if there are no errors, returns a program. It contains a tokenizer, parser, symbol table, expression evaluator, layout pass, instruction table and analyzer.
Program: the assembler's output. It contains assembled instructions with their addresses, sizes, encodings and source locations, along with initial memory, symbols and the entry point.
Interpreter (src/interpreter.rs): runs a program from memory using registers and the status register. It supports stepping, running to the end, undo history, breakpoints and a call stack. A watched memory range supports a memory-mapped device: every store to the range ends the run.
Machine (src/machine/): handles processor encodings. The decoder reads instruction words for the 68000, 68010 and 68020, the encoder writes them, and the disassembler prints EASy68K syntax. The decode cache holds instructions for the interpreter. Exception frames and cycle counts follow the selected processor.
The assembler chooses each instruction's 68000 encoding as EASy68K does (below) and writes it into the program's memory at its real size. Addresses therefore match those of a 68000 and EASy68K's listing. The interpreter runs those bytes (below, "Code is memory").
The interpreter runs the bytes in memory through a cache of decoded instructions. The cache uses 4 KB pages with one slot per word, allocated only where code exists. When the interpreter is created, the cache is filled from the assembler's instructions, so that code does not need decoding while it runs.
Every write passes through the cache, including writes by the program, trap tasks, the host and undo. A write clears the instructions it covers, which are decoded from their new bytes before they run. A program can therefore read and change its own code, as on a 68000. Undo restores the old bytes, so the old instruction runs again.
wroteCode on the step, getCodeWrite()), giving its address and line index. With runWithBreakpoints(…, {stopOnCodeWrite: true}), a run stops after a code write with status CodeWrite; the next run continues. A Poke, a testcase's starting memory and undo never report a code write.getInstructionAt result as assembled. disassembleAt(address) instead prints the current contents of memory. getInstructionAt also reports an assembler-selected form that differs from the source (assembledAs): add.l #1,d0 is ADDQ.L #1,D0, bra loop is BRA.S loop, and lea $1000,a0 is LEA $1000.W,A0.& ADDRMASK. An access past $FFFFFF wraps to $000000: this applies to a long at $FFFFFE, a stack pushed below $000000, instruction words crossing the end, strings read and lines written by trap tasks, and host reads and writes. File task buffers are the exception, as in EASy68K: a read or write past the end returns 2 (below). The map, decode cache and watched range see the bytes at their wrapped addresses. Only a run of bytes longer than the whole 16 MB is refused as out of bounds.$FFFF $FFFF is EASy68K's SIMHALT wherever it occurs. Within bytes placed by the program, whether code or data, it pauses execution like simhalt. Elsewhere it ends the program with EndOfProgram and the address. Memory starts as $FF, so a program that does not end itself stops there, one past its last instruction if nothing follows it. Other bytes execute as the instructions they decode to: execution that reaches or jumps into data runs that data, and a word that is not an instruction raises an illegal instruction exception. Fetching an instruction at an odd address raises an address error; a jump to an odd address raises it in the jump itself, as on a 68000.The machine follows the 68000, including where EASy68K differs from it. Every exception is taken through the vector table at $000 to $3FF:
| Vector | Address | Raised by | Stacked PC |
|---|---|---|---|
| 2 | $008 |
a bus error: an access the memory map refuses (below, "The memory map") | as for an address error at the same access; an instruction that could not be fetched, its own |
| 3 | $00C |
an address error: a word or long at an odd address, an instruction fetched from one, a jump to one | where the prefetch had got to, the 68000's (below) |
| 4 | $010 |
illegal, and a word that is no instruction |
the instruction's |
| 5 | $014 |
divs or divu by zero |
the next instruction's |
| 6 | $018 |
chk out of bounds |
the next instruction's |
| 7 | $01C |
trapv with V set |
the next instruction's |
| 8 | $020 |
a privileged instruction in user mode: move <ea>,sr, andi/ori/eori #n,sr, rte, stop, reset, move usp |
the instruction's |
| 9 | $024 |
trace: after an instruction that started with T set | the next instruction's, or the handler's of the exception it raised |
| 10, 11 | $028, $02C |
a line A word $Axxx and a line F word $Fxxx |
the instruction's |
| 25 to 31 | $064 to $07C |
an interrupt request at level 1 to 7, through its autovector (below, "Interrupts") | the instruction it came before |
| 32 to 46 | $080 to $0B8 |
trap #0 to trap #14 |
the next instruction's |
| 47 | $0BC |
a trap #15 task that is none of EASy68K's |
the next instruction's |
The 68010 uses the same table and adds bkpt and movec of a register it does not have as causes of vector 4. An rte format error uses vector 14 at $038. The processor reads vectors through VBR, pushes 68010 frames, and stacks the faulting instruction's address for address and bus errors (below, "The 68010").
The 68020 has the 68010's exceptions, adds chk2 and trapcc alongside chk and trapv, and uses 68020 frames. It permits data reads and writes at odd addresses, so its only address error is an instruction fetch at an odd address (below, "The 68020's supervisor model").
rte pops six bytes; a bus or address error handler removes the other eight itself.Exception {cause, vector}. Memory starts as $FF, so each vector reads $FFFFFFFF until the program writes it. A program that installs no handler therefore ends when an exception is raised.Halted {cause, vector}. This happens if a frame is pushed on an odd supervisor stack pointer or into a range refused by the memory map, if a vector is read from an invalid range, or if the map refuses to fetch a bus or address error handler. Other exception handlers are fetched by the next step. If that fetch causes a bus error and its handler cannot be fetched, the processor halts in turn.$2000, A7 as the supervisor stack pointer at $01000000, and the user stack pointer at $00FF0000. A7 holds the stack pointer for the mode selected by S. A write to SR, an exception or rte can swap the two. getSystem() returns {sr, usp, ssp}; setSr, setUsp and setSsp write them and are journaled inside a poke. The 68020 has a third, master stack pointer selected by M (below, "The 68020's supervisor model"). On a 68000, move sr,<ea> is not privileged and trap #15 works in user mode, as in EASy68K.trap, trapv, chk or division by zero, the trace frame is pushed on top of that exception's frame. The trace handler runs first and returns to the other handler, as on a 68000; EASy68K pushes the frames in the opposite order. A trap #15 task that waits for the host is traced after it is answered.bsr has pushed its return address, but jsr has not. A data access stacks the address reached by prefetch, which depends on the operands. A word access through (An)+ has moved the register; a long access has not. clr and move sr,<ea> read their destination first, so an odd destination faults as a read. The rules are in src/machine/exception.rs.kind: 'exception', its vector, the error name in cause, and the vector base register used in vbr. Like a call's frame, it is gone once the stack pointer of the stack it was pushed on is above it, which rte does, and so does an rtr that pops it. The step records PushException (with the same cause and vbr) and PopException, and stack swaps through the WriteStackPointers mutation (which identifies the stack pointer A7 became). Undo reverses all of it.The 68000's interrupt requests, IRQs, are EASy68K's without its hardware window: seven levels, all autovectored, from timers, the keyboard and the mouse.
$60 plus four times the level. The exception pushes a six-byte frame on the supervisor stack, sets S, clears T and raises the mask to the level. A handler can therefore be interrupted only by a higher level or level 7. Taking an IRQ is a separate execution step with kind: 'irq' and no location; History shows it and Undo reverses it on its own. An autovector with no handler ends the program with Irq {level} as the cause and identifies the vector.trap, chk, trapv or division by zero are pushed first, and the IRQ handler runs before their handlers. An illegal instruction at the program counter has not yet been fetched, so it raises its exception after the IRQ handler's rte. A request arriving while a trap #15 task waits for the host is taken after the answer. EASy68K loses an interrupt taken beside a trap and runs an illegal instruction twice.stop #n is privileged and loads the whole status register, including T and S. The processor waits for a request above the new mask, with status Stopped and the program counter at the next instruction. While it waits, step() and every run return Stopped immediately. A request that can wake it changes the status to Running immediately, and the next step takes the request. getMachineState().stopped is true until then. If a request above the new mask is already pending, stop does not wait. If it starts with T set, it takes the trace exception instead of stopping, as on a 68000. With S clear in the immediate, the processor stops in user mode; EASy68K raises a privilege violation instead. EASy68K also runs one more instruction before the interrupt that wakes it.reset is privileged and takes 132 cycles but does nothing else (below, "Cycles"). There are no devices behind the reset line, and nothing runs the 68000's reset sequence.