SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

Cardigan: a CARDIAC cardboard computer simulator and IDE

Cardigan, cardiac goes animated: the word Cardigan over CARDIAC's instruction clock, ten coloured discs joined in pairs. Cardigan stepping through a multiplication program: the listing with the current line highlighted, the help pane's instruction clock, and memory as glowing nixie tubes with an arrow showing a value about to move to the accumulator.

Cardigan is an IDE for CARDIAC that runs in your browser. Write a program, run it, and step through it one instruction at a time while the machine's memory, accumulator and cards move in front of you. There is nothing to install, and it works offline once loaded.

What you can do

Getting started

  1. Open Cardigan. The first time, a welcome box offers a short tour.
  2. Pick an example from the Explorer, such as 02-countdown.
  3. Press F5 to run it, or F11 to take one step at a time. The output appears in the Debug Console.
  4. Change a line and run it again, or choose New in the Explorer to write your own.

If you've used vscode, you already know your way around: the Explorer and Run and Debug on the left, the editor in the middle, the Debug Console and Memory at the bottom, and the status bar along the bottom edge.

Documentation

The machine in thirty seconds

  • 100 memory cells, 00 to 99, each holding a signed three-digit number, -999 to 999.
  • An accumulator of four digits and a sign.
  • A program counter, the PC: the cell whose instruction runs next. The manual calls it "the bug".
  • Ten instructions. The first digit of a word is the opcode and the other two are an address.
  • The instruction clock, from Cardigan's help pane, puts the ten opcodes round a dial, joined across the middle. Opcodes five apart do opposite jobs: INP and OUT, CLA and STO, ADD and SUB, TAC and JMP (a jump if, a jump always), SFT and HRS.
0INPINP1CLACLA2ADDADD3TACTAC4SFTSFT5OUTOUT6STOSTO7SUBSUB8JMPJMP9HRSHRS
CodeMnemonicWhat it does
0INPreads the next input card into the cell
1CLAclears the accumulator and adds the cell
2ADDadds the cell to the accumulator
3TACjumps to the cell if the accumulator is negative
4SFTSFT xy shifts the accumulator x places left, then y right
5OUToutputs the value in the cell
6STOstores the accumulator's last three digits in the cell
7SUBsubtracts the cell from the accumulator
8JMPjumps to the cell, leaving the way back (8xx) in cell 99
9HRShalts, with the PC left on the cell

Cell 00 always holds 001. That is what lets a deck of cards load itself.

Writing a program

Programs are listings in the six-column layout from Brian Stuart's CARDIAC page:

ADDRESS  CONTENTS  LABEL  OPCODE  OPERAND  COMMENT
10       155       start  CLA     k6       x = 6
11       650              STO     x

The editor is plain text: type, select, cut, copy and paste as anywhere else. It also accepts the manual's own tables (address, contents, comments) as printed, so you can paste a program straight out of the book.

Writing with mnemonics. You don't have to work out the contents yourself:

  1. Press Enter at the end of a line. The new line's address is filled in: the next cell.
  2. Type a mnemonic such as STO, then Tab. It goes in the opcode column and the cursor moves to the operand.
  3. Type the operand: a number (00 to 99), a label, or label+N. The contents fill in as you type: 651 for STO y when y is cell 51.

To label a line, type the label first, then Tab, then the mnemonic. DATA works like a mnemonic for cells that hold a number: DATA Tab 7 makes the cell 007.

Errors are underlined, red for an error and yellow for a warning; hover to read why. A line with an error isn't loaded into memory until it's fixed. Contents you type by hand are taken as written; if they disagree with the opcode and operand, the line gets a warning and Ctrl+. offers to fix it.

Editor keys

KeyDoes
Ctrl/Cmd+Ssave
Ctrl/Cmd+Ffind
Ctrl/Cmd+Z, Ctrl/Cmd+Shift+Zundo, redo
Ctrl/Cmd+.quick fix: correct stale contents, renumber clashing lines
F2rename a label everywhere
Ctrl/Cmd+clickgo to a label's definition

Files

Your work is kept in the browser between visits. A dot on a tab means unsaved changes.

Loading and the PC

A listing goes straight into memory, the manual's short cut of writing a program directly into the cells. The PC is put on the program's start: the cell a Start at NN. line names, or else the line labelled start, or else the first instruction.

A deck loads through the bootstrap, as it would in a real CARDIAC's reader. By default the loading happens instantly and the debugger stops on the program's first instruction; turn on Step through loading to watch the bootstrap read the cards.

To start somewhere else, drag the green arrow onto another cell or source line, click the PC in Variables and type an address, or right-click a cell or line and choose Set PC here.

Running and stepping

ButtonKeyDoes
Continue / PauseF5 / F6runs until something stops it
Step IntoF11runs exactly one instruction
Run to Cursorruns until the selected line's cell
Toggle BreakpointF9or click the gutter left of a line number
RestartCtrl+Shift+F5runs the program again from its start
StopShift+F5resets, with the PC back on the start

A run stops at a breakpoint, a halt, an INP waiting for input, or when you pause. Step Over is greyed out on purpose: CARDIAC has no call instruction, only JMP, so there is no telling a call from a jump. To get past a subroutine, put the cursor on the line after it and use Run to Cursor.

With Animate off the machine runs flat out and the display catches up when it stops. Turn it on and pick a speed to watch it run.

Seeing what's happening

Input and output

Programs in several decks

A program too big for memory can come as several decks, NAME.cqb, NAME.2.cqb and so on, opened together. A deck asks for the next by halting with the PC on its number: HRS 02 means "load deck 2". Cardigan puts that deck in the reader and starts the loader for you, in any order and as often as asked. The cells a deck doesn't load keep their values, and each deck's cells glow in their own colours.

What isn't standard CARDIAC

The machine follows the manual. Three conveniences go beyond it:

Outside the browser

Cardigan's assembler and machine are the same code as cardiaq, the open-source CARDIAC tools for the command line: carc assembles a listing into a deck, burn runs decks very fast, and ekg debugs and profiles them. A deck you export from Cardigan runs in burn, and a deck carc makes opens in Cardigan.

Feedback

Found a problem, or have an idea? Tell us on Discord, or write to bug@strangequery.com.

Open Cardigan