M68K Trap Tasks

Calling trap #15

The 68000 instruction set includes trap #n, which transfers control to an exception handler. The processor does not define what a handler should do with a request to print text, read input, or draw a shape. This editor provides a small set of services through trap #15: put the task number in D0.B, put its arguments in the registers listed below, then read any answer from the documented output registers. Other trap numbers are not implemented here.

The simulator connects your program to the editor's console, screen, keyboard, mouse, clock, and project files. Printed text appears both in the console transcript and at the screen's text cursor. Testcases compare the transcript. A screen operation (such as task 33 or tasks 80–99), keyboard poll/state task (7 or 19), or mouse read (61) switches interactive input for tasks 2, 4, 5, and 18 from the console to the focused screen panel for the rest of that run; there is no task to switch back. Click the panel before typing. Its focus ring indicates that editor keyboard shortcuts are paused while the program receives keys.

These services are part of the simulator, not instructions built into the 68000. The editor supplies only the services listed on this page. Unsupported device requests stop the program with an explanation. Undo reverses a service's changes along with the instruction's register and memory changes. Testcases run with scripted input and their own copy of the project's files.

Simulator behavior

  • A run starts with a 640 by 480 pixel screen. Graphics use pixel coordinates with (0, 0) at the top left; drawing beyond an edge is clipped. The fixed text font uses 8 by 16 pixel cells and draws ASCII glyphs only. Other Windows-1252 characters appear in the console transcript, but their screen cells are blank.
  • Text uses Windows-1252 bytes: each character occupies one byte. For example, € is $80. The screen font may not draw every character that the console can display.
  • Line and number input accept at most 79 typed characters. Extra characters are dropped. A number read accepts an optional sign and digits at the start of the line; 12abc reads as 12, and a line without a number reads as zero.
  • Keyboard and mouse input are polled by the program. The simulator does not deliver device interrupts. For keyboard input, use task 7 to check whether a key is waiting, or task 5 to wait for one key.
  • Task 8 reads hundredths of a second since local midnight. Testcases use UTC and start at 2000-01-01 midnight. Task 23 waits during a normal run; in a testcase it advances the virtual clock without making the test wait in real time.
  • File tasks use paths relative to the project root. Up to eight files can be open at once, numbered 0 through 7. File contents and positions are part of Undo history, and each testcase receives its own file copy.
  • An unsupported task or an invalid argument stops execution with an error that names the task or register. This makes unsupported hardware and invalid values visible instead of silently ignoring them.

Text I/O

tasks 0 to 20

Printing and reading. Everything printed is appended to the terminal transcript and drawn on the screen at the text cursor. Testcases compare the transcript. Typed input is echoed to both, unless task 12 turned echo off. Text uses Windows-1252, a single-byte character encoding: each character takes one byte, including € and curly quotes.

Display string with CR, LF

D0.B = 0

Displays D1.W characters of the string at (A1), at most 255, stopping early at a NUL, then a new line. See task 13 for the NUL terminated form.

In
A1 = string address, D1.W = length

Display string

D0.B = 1

Displays D1.W characters of the string at (A1), at most 255, stopping early at a NUL, without a new line. See task 14 for the NUL terminated form.

In
A1 = string address, D1.W = length

Read string

D0.B = 2

Reads a line of input, typed in the console, or on the screen once the program has used it, and ended with Enter; a testcase answers it from its scripted input. Its first 79 characters are stored, then a NUL (zero byte). Text is converted to Windows-1252, a single-byte encoding; a character it cannot represent is stored as ?.

In
A1 = buffer address
Out
The NUL terminated string at (A1), D1.L = its length
Note
Only the first 79 characters are stored. Additional typed characters are dropped.

Display signed number

D0.B = 3

Displays D1.L in decimal, in the smallest field it fits. See tasks 15 and 20.

In
D1.L = number

Read number

D0.B = 4

Reads a line and converts it by skipping leading spaces, reading an optional sign, then taking digits up to the first other character. 12abc is 12, and a line with no number, an empty one included, is 0, never an error.

Out
D1.L = number
Note
A number too long for 32 bits keeps its low 32 bits.

Read character

D0.B = 5

Reads one key as soon as it is typed, without waiting for Enter, in the console or on the screen once the program has used it. Enter is $0D. Check task 7 first to poll instead of waiting.

Out
D1.B = the character, $0D for Enter

Display character

D0.B = 6

Displays one character.

In
D1.B = ASCII code

Terminate

D0.B = 9

Ends the program.

Set or get the text cursor, or clear the screen

D0.B = 11

The text cursor is where printed text lands, in character cells counted from the top left. Clearing with $FF00 clears text and graphics together, since they share one image, and homes the cursor. Positions outside the screen are clamped to it.

In
D1.W = $FF00 to clear, $00FF to get, otherwise column in the high byte and row in the low byte
Out
For $00FF, D1.W = column in the high byte, row in the low byte

Keyboard echo

D0.B = 12

Whether input read by tasks 2, 4, 5 and 18 is echoed in the console and on the screen. With echo off, a line read still moves to a new line when Enter ends it, and a key read shows nothing. Echo is on when a program starts, and Undo restores the previous setting.

In
D1.B = 0 to turn the echo off, anything else to turn it on

Display NUL terminated string with CR, LF

D0.B = 13

Displays the NUL terminated string at (A1), then a new line.

In
A1 = string address

Display NUL terminated string

D0.B = 14

Displays the NUL terminated string at (A1) without a new line.

In
A1 = string address

Display unsigned number in a base

D0.B = 15

Displays D1.L as an unsigned number in the base in D2.B, using upper case digits past 9: 255 in base 16 is FF.

In
D1.L = number, D2.B = base (2 to 36)
Note
A base outside 2 to 36 stops the program with an error naming D2.B.

Input prompt and line feed

D0.B = 16

Controls the prompt shown while input is waiting and whether a key read of Enter adds a line feed. The waiting prompt appears as a caret and question in the console; the screen does not draw a flashing cursor. Without the line feed, Enter takes the screen text cursor back to the start of its line. Line and number reads always start a new line when Enter ends them, even with echo or line-feed settings off. Both settings are on when a program starts, and Undo restores the previous setting.

In
D1.B = 0 to hide the input prompt, 1 to show it, 2 to turn the line feed after Enter off, 3 to turn it on
Note
Any other D1.B value stops the program with an error naming the value.

Display string and number

D0.B = 17

Task 14 then task 3: the NUL terminated string, then the signed number.

In
A1 = string address, D1.L = number

Display string and read number

D0.B = 18

Task 14 then task 4: the NUL terminated string as a prompt, then a number.

In
A1 = string address
Out
D1.L = number

Display signed number in a field

D0.B = 20

Task 3 right justified in a field D2.B columns wide. D2.B is signed, and a negative width left justifies the number in a field that wide instead. A number too long for the field is displayed in full.

In
D1.L = number, D2.B = field width

Graphics

tasks 33 to 96

Drawing on the screen. The origin is the top left, coordinates are signed pixels, so a shape may start off the left or the top, and whatever falls outside the screen is clipped. Colors are $00BBGGRR longs. Rectangles and ellipses exclude their right and bottom edges.

Colors

A color is a long written $00BBGGRR: blue in bits 23-16, green in bits 15-8 and red in bits 7-0. The color names shown here are also available as equates to use in assembly programs.

  • black $00000000
  • maroon $00000080
  • green $00008000
  • olive $00008080
  • navy $00800000
  • purple $00800080
  • teal $00808000
  • gray $00808080
  • red $000000FF
  • lime $0000FF00
  • yellow $0000FFFF
  • blue $00FF0000
  • fuchsia $00FF00FF
  • aqua $00FFFF00
  • ltgray $00C0C0C0
  • white $00FFFFFF

Set or get the screen size

D0.B = 33

Resizes the screen and clears it. The minimum is 640 by 480, which is also the size a program starts with. Windowed and full-screen requests are accepted and ignored because the screen is an editor panel.

In
D1.L = width in the high word and height in the low word, or 0 to get, 1 for windowed, 2 for full screen
Out
For D1.L = 0, D1.L = width in the high word, height in the low word

Set pen color

D0.B = 80

The color lines, outlines, pixels and text are drawn in.

In
D1.L = color as $00BBGGRR

Set fill color

D0.B = 81

The color the insides of rectangles and ellipses and a flood fill are drawn in.

In
D1.L = color as $00BBGGRR

Draw pixel

D0.B = 82

One pixel in the pen color. The pen width does not apply and the drawing point does not move.

In
D1.W = X, D2.W = Y

Get pixel color

D0.B = 83

Reads the pixel of the image being drawn on, which with double buffering is the off screen one. Outside the screen it answers with the background color.

In
D1.W = X, D2.W = Y
Out
D0.L = color as $00BBGGRR

Draw line

D0.B = 84

A line in the pen color, leaving the drawing point at X2, Y2.

In
D1.W = X1, D2.W = Y1, D3.W = X2, D4.W = Y2

Draw line to

D0.B = 85

A line in the pen color from the drawing point to X, Y, which becomes the new drawing point: a polyline is one task per point.

In
D1.W = X, D2.W = Y

Move to

D0.B = 86

Moves the drawing point without drawing.

In
D1.W = X, D2.W = Y

Draw rectangle

D0.B = 87

Filled with the fill color and outlined with the pen. The right and bottom edges are excluded, so a rectangle whose edges meet draws nothing.

In
D1.W = left X, D2.W = upper Y, D3.W = right X, D4.W = lower Y

Draw ellipse

D0.B = 88

The ellipse inscribed in that rectangle, filled with the fill color and outlined with the pen. A square bounding rectangle draws a circle.

In
D1.W = left X, D2.W = upper Y, D3.W = right X, D4.W = lower Y

Flood fill

D0.B = 89

Spreads the fill color from X, Y over every neighbouring pixel of the color that was there, four ways.

In
D1.W = X, D2.W = Y

Draw unfilled rectangle

D0.B = 90

The outline of task 87 in the pen color, with nothing inside.

In
D1.W = left X, D2.W = upper Y, D3.W = right X, D4.W = lower Y

Draw unfilled ellipse

D0.B = 91

The outline of task 88 in the pen color, with nothing inside.

In
D1.W = left X, D2.W = upper Y, D3.W = right X, D4.W = lower Y

Set drawing mode

D0.B = 92

Mode 4 draws normally and is the default; mode 2 moves the drawing point without changing any pixel; mode 16 turns double buffering off and mode 17 turns it on, so drawing goes to an off screen image until task 94 shows it.

In
D1.B = 2, 4, 16 or 17
Note
Bitwise modes (0, 1, 3 and 5 to 15) stop the program with an error naming the mode. Double buffering is available for drawing animated sprites.

Set pen width

D0.B = 93

The width of lines and of the outlines of rectangles and ellipses. A single pixel (task 82) ignores it.

In
D1.B = width in pixels

Repaint the screen

D0.B = 94

Shows the off screen image drawn under mode 17. With double buffering off it does nothing but ask for a repaint.

Draw text at a pixel position

D0.B = 95

Draws the string in the pen color with its top left corner at X, Y, over whatever is already there, so a label can sit on a drawing. Control characters are ignored. Text printed with the text tasks lands at the text cursor instead (task 11).

In
A1 = NUL terminated string, D1.W = X, D2.W = Y

Get the drawing point

D0.B = 96

Where the next line-to would start.

Out
D1.W = X, D2.W = Y

Keyboard and mouse

tasks 7 to 61

Polled input from the focused screen. There are no input interrupts: a program asks for the state it wants when it wants it (tasks 60 and 62 are therefore not supported).

Key codes

  • A letter key is the ASCII code of its capital, so A is $41 and Z is $5A. Shift, Alt and Ctrl do not change it.
  • A top row digit is its ASCII code, so 0 is $30 and 9 is $39.
  • The function keys are contiguous from F1, so F1 is $70 and F12 is $7B.
  • The keypad digits with Num Lock on are contiguous from $60.
KeyCode
Backspace$08
Tab$09
Enter$0D
Shift$10
Ctrl$11
Alt$12
Caps Lock$14
Esc$1B
Space$20
Page Up$21
Page Down$22
End$23
Home$24
Left arrow$25
Up arrow$26
Right arrow$27
Down arrow$28
Insert$2D
Delete$2E
Semicolon$BA
Equals$BB
Comma$BC
Minus$BD
Period$BE
Slash$BF
Backquote$C0
Open bracket$DB
Backslash$DC
Close bracket$DD
Quote$DE

Check for keyboard input

D0.B = 7

Polls without consuming anything: the character it reports is the one task 5 or task 2 reads next. A testcase reports its remaining scripted input the same way.

Out
D1.B = 1 when a character is waiting, 0 otherwise

Get key state

D0.B = 19

Reads whether up to four keys are held right now, one byte of the answer per key code in the same order; with D1.L = 0 it answers with the last key released and the last key pressed instead. A key held down is reported at least once however briefly it was tapped, so a polling loop never misses one.

In
D1.L = four key codes, or 0 for the last keys
Out
D1.L = four $FF/$00 bytes, or the last key up in the high word and the last key down in the low word

Enable or disable the simulator shortcut keys

D0.B = 24

Accepted and ignored: the screen panel already hands every key it takes to the program, so there are no simulator shortcuts to give up.

In
D1.L = 0 to enable, 1 to disable

Read the mouse

D0.B = 61

The pointer position is in screen pixels with the same origin drawing uses, whatever the panel’s zoom. The release and press states persist until the next one, so a program that polls slowly still sees every click; the double bit is only ever set on a press.

In
D1.B = 0 for the current state, 1 for the last button release, 2 for the last button press
Out
D0.B = Ctrl, Alt, Shift, Double, Middle, Right, Left from bit 6 down; D1.L = Y in the high word, X in the low word

Program time

tasks 8 to 23

Waiting and reading the clock. A delay suspends the program without blocking the editor, so Stop still answers and the screen still repaints while it runs. Testcases run on a virtual clock, where a delay completes at once and the clock starts at zero.

Get time

D0.B = 8

The time the program has been running, in hundredths of a second.

Out
D1.L = hundredths of a second
Note
Returns hundredths since local midnight; Testcases use UTC midnight at 2000-01-01. The value wraps at midnight.

Delay

D0.B = 23

Lets D1.L hundredths of a second of program time pass. The editor stays responsive throughout, and the screen is repainted, so this is how an animation paces itself.

In
D1.L = hundredths of a second
Note
A testcase runs on a virtual clock: the delay completes immediately and advances that clock instead of waiting.

Files

tasks 50 to 59

Reading and writing the Project’s Files. A path is a NUL-terminated string (ended by a zero byte) of at most 255 characters, relative to the Project root, with / or \ between its parts. At most eight files are open at once, numbered 0 to 7, and D0.W reports the result: 0 success, 1 end of file, 2 error, 3 read only. Undo restores changes made by a file task, and each testcase works on its own copy of the Files.

Close all files

D0.B = 50

Closes every file the program has open.

Out
D0.W = 0

Open an existing file

D0.B = 51

Opens the existing File at the path at its start. D0.W is 0 when it opens for reading and writing, or 3 when it opens for reading only. A path with no File, a Directory, or a ninth open file is an error.

In
A1 = NUL terminated path
Out
D1.L = file number, or -1; D0.W = 0, 3 when it opened for reading only, 2 when it did not open

Open a new file

D0.B = 52

Creates the File at the path, or empties the one that is there, and opens it for reading and writing. The Directories on its path are created with it.

In
A1 = NUL terminated path
Out
D1.L = file number, or -1; D0.W = 0, or 2 when it did not open

Read a file

D0.B = 53

Reads up to D2.L bytes from the file’s position into the buffer, and moves the position past them. Fewer bytes than asked for is a success, with their count in D2.L; none at all is the end of the file, which leaves D2.L as it was.

In
D1.L = file number, A1 = buffer address, D2.L = byte count
Out
D2.L = bytes read; D0.W = 0, 1 at the end of the file, 2 on an error
Note
A read of no bytes, D2.L = 0, reports 2, including when an earlier read reached the end of the file.

Write a file

D0.B = 54

Writes the D2.L bytes at (A1) at the file’s position, growing the File as needed, and moves the position past them. A write of no bytes, and one to a file opened for reading only, report 2.

In
D1.L = file number, A1 = buffer address, D2.L = byte count
Out
D0.W = 0, or 2 on an error

Position a file

D0.B = 55

Moves the file’s position to D2.L bytes from its start. A position past the end reads as the end of the file until a write there fills the gap with zeros; a negative one is an error.

In
D1.L = file number, D2.L = position
Out
D0.W = 0, or 2 on an error

Close a file

D0.B = 56

Closes the file, so that its number can be given to the next one opened.

In
D1.L = file number
Out
D0.W = 0, or 2 when it was not open

Delete a file

D0.B = 57

Removes the File at the path from the Project. A file the program still has open stays readable and writable through its number until it is closed.

In
A1 = NUL terminated path
Out
D0.W = 0, or 2 when there is no File there
Note
A file can be deleted while it is open and remains readable through its file number until closed.

File dialog

D0.B = 58

Asks for the path of a File, for the program to open with task 51 or create with task 52. Cancel, or an empty answer, leaves (A3) alone and puts 0 in D1.L. A testcase answers it with its next scripted input.

In
D1.L = 0 to open, 1 to save; A1 = title, A2 = filter, either 0 for none; A3 = a 256 byte buffer
Out
The path chosen at (A3), NULs to 256 bytes; D1.L = 1, or 0 when the dialog was cancelled; D0.W = 0, or 2 when the buffer runs past the end of memory
Note
Shows a text prompt naming the title and asking for a path from the Project root. A D1.L other than 0 or 1 stops the program with an error naming the value.

Check that a file exists

D0.B = 59

Whether there is a File at the path. A Directory is not one, so it answers 2.

In
A1 = NUL terminated path
Out
D0.W = 0 when the File can be written, 3 when it can only be read, 2 when there is none

Unsupported tasks

Each task listed here stops the program with an error naming the task and the unsupported operation.

  • 10 print to the printer — the editor has no printer
  • 21 font properties — the screen draws text in one fixed cell font
  • 22 read a character from the text screen, 25 scroll a text rectangle — the screen holds pixels, not a grid of characters
  • 30 clear the cycle counter, 31 read the cycle counter — instruction timing is not modeled, so these tasks cannot report a 68000 cycle count
  • 32 hardware and simulator control — there is no hardware window and no automatic IRQ
  • 40 initialize a serial port, 41 set the serial port parameters, 42 read a string from a serial port, 43 send a string to a serial port — no serial device is available to a program
  • 60 enable the mouse IRQ — mouse input is polled with task 61, not delivered as an interrupt
  • 62 enable the keyboard IRQ — keyboard input is polled with tasks 7 and 19, not delivered as an interrupt
  • 70 play a sound file, 71 load a sound file, 72 play a loaded sound, 73 play a sound file with DirectX, 74 load a sound file with DirectX, 75 play a loaded DirectX sound, 76 control the sound player, 77 control the DirectX sound player — the editor provides no audio output for these tasks
  • 100 create a network client, 101 create a network server, 102 send over the network, 103 receive from the network, 104 close a network connection, 105 get the local IP address, 106 send on a network port, 107 receive data and its port — programs in this editor cannot open network connections