MCPcopy Create free account
hub / github.com/Specy/s68k

github.com/Specy/s68k @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
581 symbols 1,228 edges 25 files ⚖ MIT 42 documented · 7% ● updated 16d ago★ 34

Browse by type

⨍ Functions 477◇ Types & classes 104
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

s68k

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:

Purpose

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.

Architecture

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").

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.

  • A code write occurs when a program store or a trap task's answer writes into an instruction that has been decoded. Each step reports the first instruction overwritten (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.
  • Source locations come from the program's map of the lines it laid out. The map does not move when bytes change: a changed line keeps its location, breakpoint and 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.
  • The 24-bit bus. Memory covers the 16 MB reachable by the 68000's 24 address lines. Addresses use their low 24 bits, as in EASy68K's & 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.
  • The end. $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.

Exceptions

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").

  • Taking an exception. The processor reads the vector, copies the status register, sets S, clears T, pushes a frame on the supervisor stack and jumps to the handler. A normal frame is six bytes: the status register followed by the program counter. A bus or address error frame is 14 bytes: the special status word, access address, instruction register, status register and program counter. The special status word contains bits 15 to 5 of the instruction register, R/W, I/N clear, and the function code for the mode and space of the access. rte pops six bytes; a bus or address error handler removes the other eight itself.
  • No handler. An odd address in a vector means there is no handler. The program ends with the error and vector in 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.
  • A double fault halts the processor with 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.
  • Supervisor and user mode. A program starts in supervisor mode with EASy68K's registers: SR $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.
  • Trace. T is sampled before an instruction. If it was set, a trace exception follows the instruction unless it raised an illegal instruction, privilege violation, line A or F, or address error and did not finish. After 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.
  • Address error PC and side effects. These follow the 68000 behavior left by MAME's microcode for the 55,606 address errors in the conformance suite. A jump to an odd address faults in its own prefetch: 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.
  • The call stack gains a frame for each exception, with 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.

Interrupts

The 68000's interrupt requests, IRQs, are EASy68K's without its hardware window: seven levels, all autovectored, from timers, the keyboard and the mouse.

  • Latched. A request at level 1 to 7 waits in a latch until it is taken. A second request at an already pending level is the same request.
  • Taken between instructions. Before the next instruction, the processor takes the highest pending level above the status register's interrupt mask. Level 7 is taken regardless of the mask. The autovector is vector 24 plus the level, at $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.
  • Other exceptions. As on a 68000, an instruction's trace frame and the frame for 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.
  • The sources. The program configures in

Core symbols most depended-on inside this repo

browse all functions →

Shape

Method 360
Function 117
Class 69
Enum 35

Languages

Rust55%
TypeScript45%

Modules by API surface

pkg/s68k_bg.js155 symbols
src/interpreter.rs123 symbols
ts-lib/src/index.ts74 symbols
src/lexer.rs39 symbols
src/semantic_checker.rs31 symbols
src/compiler.rs29 symbols
src/debugger.rs28 symbols
pkg/s68k.d.ts21 symbols
src/lib.rs17 symbols
src/instructions.rs17 symbols
src/utils.rs12 symbols
web/src/index.ts11 symbols

For agents

$ claude mcp add s68k \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page