Aldan manual

The hardware cheatsheet

ortfero

The machine as a program sees it: the processor, the memory map, and the registers of every device. It is enough to bring the machine up and to drive each device from nothing. Addresses, offsets, ids, codes and register values are hexadecimal; counts, sizes and dimensions are decimal, but for the sizes in the memory map.

The machine

The processor

Instructions

VWhat there is
vtypeSEW 8, 16, 32, 64; LMUL 1, 2, 4, 8. Fractional LMUL sets vill. vta and vma are ignored: tails and masked elements are left undisturbed
Configvsetvli, vsetivli, vsetvl
MemoryUnit-stride, strided and indexed, EEW equal to SEW; vl1r.v and vs1r.v only. No vlm, no fault-only-first, no segments
Integervadd, vsub, vrsub, vand, vor, vxor, vsll, vsrl, vsra, vmin(u), vmax(u), vmerge, vmv.v, vmseq..vmsgt, vsadd(u), vssub(u), vsmul, vssra, vaadd, vmul, vmulh(u, su), vmacc, vnmsac, vwmul(u, su), vwadd(u), vslideup, vslidedown, vslide1up, vslide1down
Reduce, maskvredsum, vredmax(u), vredmin(u), vcpop.m, vfirst.m, the eight mask logicals, vmv.x.s, vmv.s.x
FloatSEW 32 and 64: vfadd, vfsub, vfmul, vfdiv, vfmin, vfmax, vfsgnj(n), vfmacc, vfsqrt, vfcvt, vfmv, vfredusum
MissingIllegal instruction: vdiv(u), vrem(u), vrgather, vcompress, vid, viota, vzext, vsext, vmv<n>r.v; narrowing, and widening but vwmul and vwadd; vmadd, vnmsub, vaaddu, vasub(u), vadc, vsbc, vmadc, vmsbc; vredand, vredor, vredxor, vmsbf, vmsif, vmsof; vfrsub, vfrdiv, vfsgnjx, vmfeq and the other float compares, vfclass, vfmsac and the other fused forms but vfmacc, vfredosum, vfredmax, vfredmin

Registers

The CSRs that work:

CSRNumberWhat it does
mstatus300MIE (bit 3), MPIE (7), MPP (12:11); nothing else acts
mie304MSIE (3), MTIE (7), MEIE (11)
mip344Read only, live
mtvec305Mode 0 direct, 1 vectored (interrupts at base + 4 * cause)
mscratch340Scratch
mepc341Taken raw by mret
mcause342The cause
mtval343The faulting word or pc
mhartidF140
marchidF12616C64616E34, "aldan4"
timeC01The clint's mtime, 10 MHz
instretC02Instructions retired
fflags, frm, fcsr1, 2, 3The float state
vstart, vxsat, vxrm, vcsr8, 9, A, FThe vector state
vl, vtype, vlenbC20, C21, C22Read only
tselect, tdata1-3, tinfo, tcontrol7A0 to 7A5Sdtrig: 8 triggers, mcontrol6 (execute, exact address) and icount

Any other CSR number is accepted without a trap and lands in a 128-entry array indexed by its low seven bits, so unrelated CSRs alias each other: cycle (C00) reads mstatus, and a write to satp (180), sstatus (100) or mcycle (B00) overwrites mstatus. Keep to the table.

Traps

Reset

Memory

AddressSizeWhat is there
001000001000Power switch (syscon)
001010001000Real-time clock
0200000010000Timer (clint)
0C000000400000Interrupt controller (plic)
10000000100Serial line (16550)
100010005 * 1000Virtio transports 0 to 4
30000000100000PCI configuration (ECAM)
4000000040000000PCI memory window
8000000010000Boot ROM
80010000FF0000RAM
808000001000The device tree, in RAM

Access rules

Interrupts

The plic gathers the device lines into mip.MEIP. All sources are level-triggered: a source is pending while its line is high and it is not claimed.

SourceDevice
1Virtio 0: host directory
2Virtio 1: network
3Virtio 2: keyboard
4Virtio 3: sound
5Virtio 4: display
ASerial line, receive
21NVMe (PCI slot 1, INTA)
OffsetRegister
0 + 4 * idPriority of source id, 0 to 7; 0 is never
1000Pending, one bit per source, three words
2000Enable for machine mode, three words
200000Threshold, 0 to 7
200004Read: claim, the best source or 0. Write: complete that source

Timer

The clint at 02000000 counts wall-clock time at 10 MHz.

OffsetWidthRegister
032msip: bit 0 is mip.MSIP
400064mtimecmp, all ones at power-on
BFF864mtime, writable

Clock

A goldfish real-time clock at 00101000: nanoseconds since 1970, from the host's wall clock.

OffsetRegister
0TIME_LOW, 32 bits. Read it first: it latches the high half. A write sets the time
4TIME_HIGH. Read it second; write it first

The clock has no alarm and no interrupt.

Power

One register at 00100000, written with sh: 5555 powers the machine off, 7777 reboots it. The host acts at the end of the current slice, so follow the store with a wfi loop.

Serial line

A 16550 subset at 10000000, byte registers one apart. It needs no set-up: the baud rate is ignored, and a byte written is out at once.

OffsetRegister
0Read: RBR, the next byte in. Write: THR, a byte out
1IER: bit 0 raises source A while a byte waits
2IIR: 4 while a byte waits, else 1
5LSR: 60 always (THR empty), and bit 0 while a byte waits

LCR, MCR, FCR and SCR are stored and do nothing. Read RBR until LSR bit 0 clears to drop the line.

Virtio

Five transports, virtio-mmio version 2 only, at 10001000 + 1000 * i with plic source 1 + i. A transport with device id 0 is empty: the host directory and the network are absent on some hosts, so read the id before using one.

SlotDeviceIdQueues
0Host directory90 requests
1Network10 receive, 1 transmit
2Keyboard120 events, 1 status
3Sound190 control, 1 events, 2 transmit, 3 receive
4Display100 control, 1 cursor

The transport

OffsetRegister
0Magic 74726976
4Version 2
8Device id
10, 14Device features, and its select (0 or 1)
20, 24Driver features, and its select
30Queue select
34Queue size max: 128 entries
38Queue size, a power of two
44Queue ready
50Notify: write the queue's index
60Interrupt status: 1 used ring, 2 config
64Interrupt ack: write the bits to clear
70Status
80, 84Descriptor table, low and high
90, 94Available ring, low and high
A0, A4Used ring, low and high
100The device's configuration

Keyboard

Display

virtio-gpu, 2D only. The screen is 780 x 520 times a scale of 1 or 2, or 0 x 0 with no window. Pixels are 32-bit B8G8R8X8, a little-endian xrgb word.

Every request starts with a 24-byte header: le32 type, le32 flags, le64 fence, le32 context, u8 ring, padding. The reply is a header of its own, 1100 when all is well.

TypeRequest and its fields after the header
100Display info: reply 1101 then 16 modes of {x, y, w, h, enabled, flags}; mode 0 is the screen
101Create 2D: id, format 2, width, height
106Attach backing: id, count (1 to 16), then {le64 address, le32 length, pad} each
103Set scanout: x, y, w, h, scanout 0, id
105Transfer to host: x, y, w, h, le64 offset, id, pad
104Flush: x, y, w, h, id, pad
102Unref: id, pad
107Detach backing: id, pad

Sound

virtio-sound with one output stream, id 0: signed 16-bit mono at 48 kHz, nothing else. Control requests go on queue 0 and start with le32 code, replies with le32 status: 8000 ok, 8001 bad message, 8002 not supported, 8003 I/O error.

CodeRequest
101Set params: stream, buffer bytes, period bytes, features, then u8 channels 1, format 5, rate 7, pad
102Prepare: stream
104Start: stream
105Stop: stream
103Release: stream
100Info: start, count, size; one 32-byte record

Network

virtio-net, feature MAC (bit 5) only. The configuration holds the MAC 52:54:00:12:34:56. Every frame has a 12-byte header in front of it, all zero but for a 1 at byte 10 on receive. Frames are Ethernet without the checksum, up to 1514 bytes.

The host answers as a small router:

Host directory

virtio-9p over the directory aldan in the host user's home. The configuration is le16 length then the tag "aldan". One request to a chain: the message, then room for the reply.

Disk

An NVMe controller at PCI 00:01.0, reached through ECAM at 30000000 + device * 8000, so its configuration is at 30008000. Device 0 is a host bridge; nothing else is on the bus.

OffsetRegister
0Id 00101B36
4Command: 2 memory, 4 bus master, 400 INTx off
8Class 010802
10, 14BAR0, 64-bit, 4000 bytes; nothing assigned at reset

Bring-up: write BAR0 = 40000000 and BAR1 = 0, set command to 6, then use the controller's registers at 40000000. Configuration stores are whole words.

OffsetRegister
0CAP: queues of up to 64 entries, doorbell stride 4, 4 KB pages
14CC: write 00460001 to enable
1CCSTS: bit 0 ready, bit 1 fatal
24AQA: admin completion size - 1 at 16, admin submission size - 1 at 0
28, 30ASQ, ACQ: 64-bit page addresses
1000Doorbells: admin submission tail, admin completion head, then the same for queue 1