diff --git a/docs/progress.md b/docs/progress.md index 507999b..293155e 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -27,7 +27,7 @@ - [x] `make install` puts VisorMesh on the Visor; app launches and shows the main form on the Visor Neo - [ ] Emulator: CloudpilotEmu with a Palm OS 3.5 ROM (the Visor isn't emulated); optionally POSE + a Visor Neo ROM in a Windows VM - [ ] Original Handspring SDK headers (Springboard `HsCard*` APIs; the PalmOne `HsExt.h` lacks them), needed for module-ROM work only -- [ ] Port springSD bus routines; SC16IS740 driver +- [x] Bus SPI routines and SC16IS740 driver (`sb_spi.c`, `transport_sb.c`); host-tested against a simulated carrier, not on hardware - [x] Companion protocol: framing, builders (DEVICE_QUERY, APP_START, GET_CONTACTS, SYNC_NEXT_MESSAGE), parsers (SELF_INFO, contacts, v3 contact/channel messages); host tests from real captures + MeshCore layouts - [x] USB bench link: app (`HsUd`) → Visor USB cradle → `tools/visor_bridge.py` → FTDI → XIAO D6/D7. Query radio shows `Reply 0x000D, 82 bytes` on the Visor - [x] Connect screen: DEVICE_QUERY + APP_START via session.c; shows node, freq, BW/SF/CR, TX power, firmware (verified on the Neo over the USB bridge) @@ -78,3 +78,4 @@ - Docker Desktop's amd64 emulation hung once (containers never started); make install now only uses Docker when sources changed. - Verified on the Neo: two-line compose field (scrolls, redraws after the on-screen keyboard), Unicode symbols/emoji smiley, line breaks in messages, beep for DMs/mentions, "new" divider. Segments: 1 = 7.8 KB, 2 = 27.1 KB (the next big feature should go in a third segment); tools/check_sections.py guards the rules on every build. - 2026-11-02: Verified on the Neo against a PhillyMesh repeater: login, admin console (Read settings, typed CLI commands, Status 0x87 and Telemetry 0x8B decoded), contact detail with signal history, Add Contact, Reset Path. Segments: 9.1 / 27.7 / 12.4 KB. Upstream issues written up in docs/upstream-issues.md (none filed yet). +- 2026-11-02: SB-LoRa transport written ahead of the boards: bus-SPI bit-bang over CS0/CS1 (`sb_spi.c`), SC16IS740 setup for 115200 8N1, burst FIFO access, sleep, and a "Slot" link on the Node screen (`transport_sb.c`). `tests/test_sblora.c` checks it against a simulated carrier (latches, inverter, SC16IS740). Unknowns for bring-up: whether connector A0 is CPU A1 (the probe tries both mappings via the scratch register), the hard-coded csSlot0 base 0x28000000 (no Handspring SDK for HsCardAttrGet), and RX overruns while the app isn't polling (no IRQ handler yet). Segments: 9.6 / 27.7 / 13.1 KB. diff --git a/palm-client/Makefile b/palm-client/Makefile index 7e09b6c..c8926e3 100644 --- a/palm-client/Makefile +++ b/palm-client/Makefile @@ -10,7 +10,7 @@ APP = visormesh NAME = VisorMesh CREATOR = VMsh -SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/fontmetrics.c src/ports.c +SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/fontmetrics.c src/ports.c OBJS = $(SRCS:src/%.c=build/%.o) CC = m68k-palmos-gcc @@ -74,6 +74,8 @@ test: | build build/test_protocol gcc -std=c99 -Wall -Wextra -o build/test_text tests/test_text.c src/ui/text.c src/ui/fontmetrics.c src/protocol.c build/test_text + gcc -std=c99 -Wall -Wextra -o build/test_sblora tests/test_sblora.c src/sb_spi.c + build/test_sblora build: mkdir -p build diff --git a/palm-client/resources/visormesh.rcp b/palm-client/resources/visormesh.rcp index 239e1ee..432a008 100644 --- a/palm-client/resources/visormesh.rcp +++ b/palm-client/resources/visormesh.rcp @@ -61,8 +61,9 @@ MENUID MainMenuBar BEGIN TITLE "Node" LABEL "Link:" AUTOID AT (5 18) FONT 1 - PUSHBUTTON "Serial" ID MainSerialPush AT (50 17 40 12) GROUP MainLinkGroup - PUSHBUTTON "USB" ID MainUsbPush AT (PREVRIGHT+1 PREVTOP 40 12) GROUP MainLinkGroup + PUSHBUTTON "Serial" ID MainSerialPush AT (50 17 34 12) GROUP MainLinkGroup + PUSHBUTTON "USB" ID MainUsbPush AT (PREVRIGHT+1 PREVTOP 34 12) GROUP MainLinkGroup + PUSHBUTTON "Slot" ID MainSlotPush AT (PREVRIGHT+1 PREVTOP 34 12) GROUP MainLinkGroup LABEL "Node:" AUTOID AT (5 40) FONT 1 LABEL " " ID MainNameLabel AT (50 40) diff --git a/palm-client/src/app.c b/palm-client/src/app.c index 3445ade..bd5eee8 100644 --- a/palm-client/src/app.c +++ b/palm-client/src/app.c @@ -9,7 +9,7 @@ Boolean gAppAutoConnectTried = false; typedef struct { UInt16 version; - UInt32 port; /* VM_PORT_USB or VM_PORT_SERIAL */ + UInt32 port; /* VM_PORT_USB, VM_PORT_SERIAL or VM_PORT_SBLORA */ Int16 utcOffset; /* local minus UTC, minutes */ } AppPrefs; @@ -61,6 +61,8 @@ void AppErrorText(Err err, Char *out) StrCopy(out, "Message too long."); else if (err == serErrNotOpen) StrCopy(out, "Not connected."); + else if (err == vmErrNoModule) + StrCopy(out, "No SB-LoRa module in the slot."); else if (err == vmErrLoginFailed) StrCopy(out, "Login refused: wrong password?"); else diff --git a/palm-client/src/node_form.c b/palm-client/src/node_form.c index 325748e..c3512a5 100644 --- a/palm-client/src/node_form.c +++ b/palm-client/src/node_form.c @@ -172,7 +172,8 @@ Boolean NodeFormHandleEvent(EventType *event) switch (event->eType) { case frmOpenEvent: { - UInt16 link = (TransportGetPort() == VM_PORT_USB) ? MainUsbPush : MainSerialPush; + UInt32 port = TransportGetPort(); + UInt16 link = port == VM_PORT_USB ? MainUsbPush : port == VM_PORT_SBLORA ? MainSlotPush : MainSerialPush; FrmSetControlGroupSelection(form, MainLinkGroup, link); ShowRadioInfo(form); @@ -191,12 +192,17 @@ Boolean NodeFormHandleEvent(EventType *event) switch (event->data.ctlSelect.controlID) { case MainSerialPush: case MainUsbPush: + case MainSlotPush: /* Changing the link drops any open connection; it applies on the next Connect. */ if (SessionIsConnected()) { SessionDisconnect(); ShowRadioInfo(form); } - TransportSetPort(event->data.ctlSelect.controlID == MainUsbPush ? VM_PORT_USB : VM_PORT_SERIAL); + switch (event->data.ctlSelect.controlID) { + case MainUsbPush: TransportSetPort(VM_PORT_USB); break; + case MainSlotPush: TransportSetPort(VM_PORT_SBLORA); break; + default: TransportSetPort(VM_PORT_SERIAL); break; + } AppSavePrefs(); return true; case MainConnectButton: diff --git a/palm-client/src/resources.h b/palm-client/src/resources.h index e91ec46..2d889d3 100644 --- a/palm-client/src/resources.h +++ b/palm-client/src/resources.h @@ -6,6 +6,7 @@ #define MainConnectButton 1002 #define MainSerialPush 1004 #define MainUsbPush 1005 +#define MainSlotPush 1013 #define MainFreqLabel 1006 #define MainModeLabel 1007 #define MainPowerLabel 1008 diff --git a/palm-client/src/sb_spi.c b/palm-client/src/sb_spi.c new file mode 100644 index 0000000..d2b8e79 --- /dev/null +++ b/palm-client/src/sb_spi.c @@ -0,0 +1,125 @@ +#include "sb_spi.h" + +static UInt8 Xfer(UInt8 out) VM_SECT3; +static void Select(void) VM_SECT3; +static void Deselect(void) VM_SECT3; +static Boolean ScratchOk(void) VM_SECT3; + +static UInt32 gCs0 = SB_CS0_BASE; +static UInt32 gCs1 = SB_CS0_BASE + SB_CS_SIZE; +static UInt32 gOffNcs; /* byte offset that drives connector A17 */ +static UInt32 gOffMosi; /* ... and A8 */ + +/* nCS = NOT A17: an access with A17 high selects the chip. */ +static void Select(void) +{ + (void)SB_BUS_READ(gCs0 + gOffNcs); +} + +static void Deselect(void) +{ + (void)SB_BUS_READ(gCs0); +} + +/* + * One byte, MSB first. The CS0 read sets MOSI (= NOT A8) while keeping nCS + * low; the CS1 read raises SCLK, where the chip samples MOSI, and returns + * MISO, which only changes on the falling edge after it. + */ +static UInt8 Xfer(UInt8 out) +{ + UInt32 sel = gCs0 + gOffNcs; + UInt8 in = 0; + UInt8 mask; + + for (mask = 0x80; mask; mask >>= 1) { + (void)SB_BUS_READ((out & mask) ? sel : sel + gOffMosi); + in = (UInt8)((in << 1) | (SB_BUS_READ(gCs1) & 1)); + } + return in; +} + +/* Register byte: bit 7 = read, bits 6:3 = register, bits 2:1 = channel 0. */ +UInt8 SbReadReg(UInt8 reg) +{ + UInt8 val; + + Select(); + Xfer((UInt8)(0x80 | (reg << 3))); + val = Xfer(0); + Deselect(); + return val; +} + +void SbWriteReg(UInt8 reg, UInt8 val) +{ + Select(); + Xfer((UInt8)(reg << 3)); + Xfer(val); + Deselect(); +} + +void SbWriteFifo(const UInt8 *buf, UInt16 n) +{ + Select(); + Xfer(SB_THR << 3); + while (n--) + Xfer(*buf++); + Deselect(); +} + +void SbReadFifo(UInt8 *buf, UInt16 n) +{ + Select(); + Xfer(0x80 | (SB_RHR << 3)); + while (n--) + *buf++ = Xfer(0); + Deselect(); +} + +static Boolean ScratchOk(void) +{ + SbWriteReg(SB_SPR, 0x55); + if (SbReadReg(SB_SPR) != 0x55) + return false; + SbWriteReg(SB_SPR, 0xAA); + return SbReadReg(SB_SPR) == 0xAA; +} + +Boolean SbProbe(void) +{ + UInt8 shift; + + /* Connector A0 = CPU A1 first: the slot only does 16-bit, even-address cycles. */ + for (shift = 1; ; shift--) { + gOffNcs = 1UL << (17 + shift); + gOffMosi = 1UL << (8 + shift); + Deselect(); + if (ScratchOk()) + return true; + if (shift == 0) + break; + } + Deselect(); + return false; +} + +void SbUartInit(void) +{ + SbWriteReg(SB_IOCONTROL, 0x08); /* software reset; self-clearing */ + SbWriteReg(SB_LCR, 0x80); /* divisor latch */ + SbWriteReg(SB_DLL, 8); /* 14.7456 MHz / (16 * 8) = 115200 */ + SbWriteReg(SB_DLH, 0); + SbWriteReg(SB_LCR, 0x03); /* 8N1 */ + SbWriteReg(SB_FCR, 0x07); /* FIFOs on, both reset */ + SbWriteReg(SB_IER, 0x00); /* polled */ +} + +/* Sleep mode is an enhanced function: EFR bit 4 must be set to change IER bit 4. */ +void SbSleep(Boolean sleep) +{ + SbWriteReg(SB_LCR, 0xBF); + SbWriteReg(SB_EFR, 0x10); + SbWriteReg(SB_LCR, 0x03); + SbWriteReg(SB_IER, sleep ? 0x10 : 0x00); +} diff --git a/palm-client/src/sb_spi.h b/palm-client/src/sb_spi.h new file mode 100644 index 0000000..70a9881 --- /dev/null +++ b/palm-client/src/sb_spi.h @@ -0,0 +1,76 @@ +/* + * SB-LoRa Springboard module: bus SPI over the slot chip selects, and the + * SC16IS740 SPI-UART behind it (hardware/sb-lora/README.md, "Bus SPI"). + * + * CS0* access latches nCS = NOT A17 and MOSI = NOT A8 on its rising edge + * CS1* read one SCLK pulse; MISO comes back on D0 + * + * So one SPI bit is a CS0 read at an address that encodes nCS and MOSI, + * then a CS1 read that clocks it and returns MISO. The SC16IS740 is mode 0. + * + * Plain C with no Palm OS calls, so it also builds on the host, where + * tests/test_sblora.c supplies SimBusRead() and a simulated module. + */ +#ifndef VM_SB_SPI_H +#define VM_SB_SPI_H + +#include "protocol.h" /* UInt8 etc. on the host */ + +/* + * csSlot0 base and the default 16 MB chip-select size (Springboard Dev Guide + * §2.1). The guide says to ask HsCardAttrGet(hsCardAttrCsBase) instead; that + * needs the Handspring SDK headers, which this tree doesn't have yet. + */ +#define SB_CS0_BASE 0x28000000UL +#define SB_CS_SIZE 0x01000000UL + +/* SC16IS740 registers (general set; DLL/DLH need LCR = 0x80, EFR LCR = 0xBF). */ +#define SB_RHR 0x00 /* read */ +#define SB_THR 0x00 /* write */ +#define SB_IER 0x01 +#define SB_FCR 0x02 /* write */ +#define SB_LCR 0x03 +#define SB_MCR 0x04 +#define SB_LSR 0x05 +#define SB_SPR 0x07 +#define SB_TXLVL 0x08 +#define SB_RXLVL 0x09 +#define SB_IOCONTROL 0x0E +#define SB_DLL 0x00 +#define SB_DLH 0x01 +#define SB_EFR 0x02 + +#define SB_FIFO_SIZE 64 + +/* + * Finds the module by writing and reading back the scratch register. The + * connector's address lines may map to CPU A1 and up (word addressing) or + * to A0 and up; both are tried, and the one that works is kept. + */ +Boolean SbProbe(void) VM_SECT3; + +/* 115200 8N1 (14.7456 MHz crystal, DLL = 8), FIFOs on, interrupts off. */ +void SbUartInit(void) VM_SECT3; + +/* Low-power sleep (IER bit 4) while the link is closed, or wake. */ +void SbSleep(Boolean sleep) VM_SECT3; + +UInt8 SbReadReg(UInt8 reg) VM_SECT3; +void SbWriteReg(UInt8 reg, UInt8 val) VM_SECT3; + +/* Burst FIFO access: n bytes to THR, or n bytes from RHR, in one transaction. */ +void SbWriteFifo(const UInt8 *buf, UInt16 n) VM_SECT3; +void SbReadFifo(UInt8 *buf, UInt16 n) VM_SECT3; + +#ifdef __palmos__ +#define SB_BUS_READ(addr) (*(volatile UInt16 *)(addr)) +#else +UInt16 SimBusRead(UInt32 addr); +#define SB_BUS_READ(addr) SimBusRead(addr) +#ifndef true +#define true 1 +#define false 0 +#endif +#endif + +#endif diff --git a/palm-client/src/transport.h b/palm-client/src/transport.h index 81a83df..46d0b32 100644 --- a/palm-client/src/transport.h +++ b/palm-client/src/transport.h @@ -2,7 +2,8 @@ * Byte-stream link to the companion radio. One implementation per transport: * transport_srm.c Serial Manager: the cradle serial port, or the Visor's * USB data port bridged to a radio by tools/visor_bridge.py - * (later) SB-LoRa Springboard module via the SC16IS740 + * transport_sb.c SB-LoRa Springboard module via the SC16IS740; picked + * with VM_PORT_SBLORA, which transport_srm.c hands over */ #ifndef VM_TRANSPORT_H #define VM_TRANSPORT_H @@ -12,6 +13,9 @@ /* Serial Manager ports (see the Ports screen for what a device has). */ #define VM_PORT_SERIAL serPortCradlePort #define VM_PORT_USB 'HsUd' /* Handspring "USB" driver: the Visor's GENERIC USB port */ +#define VM_PORT_SBLORA 'SbLo' /* not a Serial Manager port: transport_sb.c */ + +#define vmErrNoModule (appErrorClass | 6) /* no SB-LoRa module answered (session.h has 1-5) */ void TransportSetPort(UInt32 port); /* takes effect on the next TransportOpen */ UInt32 TransportGetPort(void); @@ -26,4 +30,9 @@ Err TransportSend(const UInt8 *buf, UInt16 len); */ UInt16 TransportRecv(UInt8 *buf, UInt16 max, Int32 timeoutTicks, Err *errP); +Err SbTransportOpen(void); +void SbTransportClose(void); +Err SbTransportSend(const UInt8 *buf, UInt16 len); +UInt16 SbTransportRecv(UInt8 *buf, UInt16 max, Int32 timeoutTicks, Err *errP); + #endif diff --git a/palm-client/src/transport_sb.c b/palm-client/src/transport_sb.c new file mode 100644 index 0000000..86215b4 --- /dev/null +++ b/palm-client/src/transport_sb.c @@ -0,0 +1,90 @@ +/* + * Transport over the SB-LoRa Springboard module (sb_spi.c). Polled: the + * SC16IS740's 64-byte RX FIFO fills in about 5.5 ms at 115200, so bytes that + * arrive while the app isn't inside TransportRecv can be lost. An interrupt + * handler on the slot IRQ* (wired to the chip's IRQ) would fix that; it needs + * the Handspring card API. + */ +#include +#include "transport.h" +#include "sb_spi.h" + +#define SB_SEND_TICKS 200 /* give up if the TX FIFO doesn't drain for 2 s */ + +static Boolean gOpen = false; + +Err SbTransportOpen(void) +{ + if (gOpen) + return errNone; + if (!SbProbe()) + return vmErrNoModule; + SbUartInit(); + gOpen = true; + return errNone; +} + +void SbTransportClose(void) +{ + if (!gOpen) + return; + SbSleep(true); + gOpen = false; +} + +Err SbTransportSend(const UInt8 *buf, UInt16 len) +{ + UInt32 deadline = TimGetTicks() + SB_SEND_TICKS; + UInt16 n; + + if (!gOpen) + return serErrNotOpen; + while (len) { + n = SbReadReg(SB_TXLVL); + if (n == 0) { + if (TimGetTicks() >= deadline) + return serErrTimeOut; + continue; + } + if (n > len) + n = len; + SbWriteFifo(buf, n); + buf += n; + len -= n; + deadline = TimGetTicks() + SB_SEND_TICKS; + } + return errNone; +} + +/* + * Busy-polls RXLVL until data or the timeout, then keeps reading while the + * FIFO has more, up to max. Busy rather than SysTaskDelay: a one-tick sleep + * is longer than the FIFO lasts. + */ +UInt16 SbTransportRecv(UInt8 *buf, UInt16 max, Int32 timeoutTicks, Err *errP) +{ + UInt32 deadline = TimGetTicks() + (timeoutTicks > 0 ? timeoutTicks : 0); + UInt16 got = 0; + UInt16 n; + + *errP = errNone; + if (!gOpen) { + *errP = serErrNotOpen; + return 0; + } + for (;;) { + n = SbReadReg(SB_RXLVL); + if (n) + break; + if (TimGetTicks() >= deadline) + return 0; + } + while (n && got < max) { + if (n > max - got) + n = max - got; + SbReadFifo(buf + got, n); + got += n; + n = SbReadReg(SB_RXLVL); + } + return got; +} diff --git a/palm-client/src/transport_srm.c b/palm-client/src/transport_srm.c index a855b16..e524d6e 100644 --- a/palm-client/src/transport_srm.c +++ b/palm-client/src/transport_srm.c @@ -22,6 +22,8 @@ Err TransportOpen(void) { Err err; + if (gPort == VM_PORT_SBLORA) + return SbTransportOpen(); if (gPortOpen) return errNone; err = SrmOpen(gPort, VM_BAUD, &gPortId); @@ -32,6 +34,7 @@ Err TransportOpen(void) void TransportClose(void) { + SbTransportClose(); if (!gPortOpen) return; SrmClose(gPortId); @@ -44,6 +47,8 @@ Err TransportSend(const UInt8 *buf, UInt16 len) UInt32 sent = 0; UInt16 tries; + if (gPort == VM_PORT_SBLORA) + return SbTransportSend(buf, len); if (!gPortOpen) return serErrNotOpen; /* @@ -68,6 +73,8 @@ UInt16 TransportRecv(UInt8 *buf, UInt16 max, Int32 timeoutTicks, Err *errP) UInt32 avail = 0; UInt32 n; + if (gPort == VM_PORT_SBLORA) + return SbTransportRecv(buf, max, timeoutTicks, errP); *errP = errNone; if (!gPortOpen) { *errP = serErrNotOpen; diff --git a/palm-client/tests/test_sblora.c b/palm-client/tests/test_sblora.c new file mode 100644 index 0000000..42ee521 --- /dev/null +++ b/palm-client/tests/test_sblora.c @@ -0,0 +1,223 @@ +/* + * Host-side tests for the SB-LoRa bus SPI (src/sb_spi.c). Run with `make test`. + * SimBusRead() models the carrier board: two 74LVC1G80 latches (Q-bar) on + * CS0*, the inverter making SCLK from CS1*, and an SC16IS740 in SPI mode 0. + */ +#include +#include +#include +#include "../src/sb_spi.h" + +static int failures; + +#define CHECK(cond) do { \ + if (!(cond)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); failures++; } \ +} while (0) + +/* The board */ +static int present; /* module in the slot */ +static int shift; /* connector A0 = CPU A(shift) */ +static int ncs = 1, mosi; /* latch outputs */ + +/* The SC16IS740 */ +static UInt8 regs[16], dll, dlh, efr, lcr; +static UInt8 rxq[SB_FIFO_SIZE], rxn; +static UInt8 txlog[256]; +static int txn, txFree = SB_FIFO_SIZE; +static int bit, byteIndex, transactions; +static UInt8 cmd, in, out; + +static void SimReset(void) +{ + memset(regs, 0, sizeof(regs)); + lcr = 0x1D; + regs[SB_SPR] = 0xFF; + dll = dlh = efr = 0; +} + +static UInt8 RegRead(UInt8 reg) +{ + if (lcr == 0x80 && reg == SB_DLL) return dll; + if (lcr == 0x80 && reg == SB_DLH) return dlh; + if (lcr == 0xBF && reg == SB_EFR) return efr; + switch (reg) { + case SB_RHR: { + UInt8 c = rxn ? rxq[0] : 0; + if (rxn) + memmove(rxq, rxq + 1, --rxn); + return c; + } + case SB_LCR: return lcr; + case SB_TXLVL: return (UInt8)txFree; + case SB_RXLVL: return rxn; + default: return regs[reg]; + } +} + +static void RegWrite(UInt8 reg, UInt8 val) +{ + if (lcr == 0x80 && reg == SB_DLL) { dll = val; return; } + if (lcr == 0x80 && reg == SB_DLH) { dlh = val; return; } + if (lcr == 0xBF && reg == SB_EFR) { efr = val; return; } + switch (reg) { + case SB_THR: + if (txn < (int)sizeof(txlog)) + txlog[txn++] = val; + return; + case SB_LCR: + lcr = val; + return; + case SB_IER: + regs[SB_IER] = (efr & 0x10) ? val : (UInt8)(val & 0x0F); + return; + case SB_IOCONTROL: + if (val & 0x08) + SimReset(); + return; + default: + regs[reg] = val; + } +} + +UInt16 SimBusRead(UInt32 addr) +{ + if (addr >= SB_CS0_BASE && addr < SB_CS0_BASE + SB_CS_SIZE) { + UInt32 off = addr - SB_CS0_BASE; + int newNcs = !((off >> (17 + shift)) & 1); + + mosi = !((off >> (8 + shift)) & 1); + if (ncs && !newNcs) { + bit = byteIndex = 0; + out = 0; + transactions++; + } + ncs = newNcs; + return 0xFFFF; + } + if (addr >= SB_CS0_BASE + SB_CS_SIZE && addr < SB_CS0_BASE + 2 * SB_CS_SIZE) { + int miso; + + if (!present || ncs) + return (UInt16)rand(); /* MISO tri-stated: D0 floats */ + /* A read's data bytes are loaded as their first bit goes out (burst reads repeat). */ + if (bit == 0 && byteIndex > 0 && (cmd & 0x80)) + out = RegRead((cmd >> 3) & 0x0F); + miso = (out >> 7) & 1; + in = (UInt8)((in << 1) | mosi); /* SCLK rising edge */ + out <<= 1; /* falling edge: next bit */ + if (++bit == 8) { + bit = 0; + if (byteIndex == 0) + cmd = in; + else if (!(cmd & 0x80)) + RegWrite((cmd >> 3) & 0x0F, in); + byteIndex++; + } + return (UInt16)(0xFFFE | miso); + } + printf("FAIL: bus access outside the slot at 0x%08lx\n", (unsigned long)addr); + failures++; + return 0; +} + +static void SimInsert(int mapping) +{ + present = 1; + shift = mapping; + SimReset(); + rxn = 0; + txn = 0; + txFree = SB_FIFO_SIZE; +} + +static void test_probe(void) +{ + SimInsert(1); + CHECK(SbProbe()); + CHECK(ncs); + SimInsert(0); + CHECK(SbProbe()); + present = 0; + CHECK(!SbProbe()); +} + +static void test_uart_init(void) +{ + SimInsert(1); + CHECK(SbProbe()); + regs[SB_SPR] = 0x12; + SbUartInit(); + CHECK(dll == 8 && dlh == 0); + CHECK(lcr == 0x03); + CHECK(regs[SB_FCR] == 0x07); + CHECK(regs[SB_IER] == 0); + CHECK(regs[SB_SPR] == 0xFF); /* the reset happened */ + CHECK(ncs); +} + +static void test_registers(void) +{ + SimInsert(1); + CHECK(SbProbe()); + SbWriteReg(SB_SPR, 0x3C); + CHECK(regs[SB_SPR] == 0x3C); + CHECK(SbReadReg(SB_SPR) == 0x3C); + txFree = 17; + CHECK(SbReadReg(SB_TXLVL) == 17); +} + +static void test_fifos(void) +{ + static const UInt8 frame[] = { '<', 2, 0, 0x16, 0x03 }; + UInt8 buf[SB_FIFO_SIZE]; + int before, i; + + SimInsert(1); + CHECK(SbProbe()); + SbUartInit(); + before = transactions; + SbWriteFifo(frame, sizeof(frame)); + CHECK(transactions == before + 1); + CHECK(txn == (int)sizeof(frame) && memcmp(txlog, frame, sizeof(frame)) == 0); + + for (i = 0; i < 40; i++) + rxq[i] = (UInt8)(0xA0 + i); + rxn = 40; + CHECK(SbReadReg(SB_RXLVL) == 40); + SbReadFifo(buf, 30); + for (i = 0; i < 30; i++) + CHECK(buf[i] == (UInt8)(0xA0 + i)); + CHECK(rxn == 10); /* exactly 30 popped */ + SbReadFifo(buf, 10); + CHECK(buf[0] == 0xA0 + 30 && buf[9] == 0xA0 + 39); + CHECK(SbReadReg(SB_RXLVL) == 0); + CHECK(ncs); +} + +static void test_sleep(void) +{ + SimInsert(1); + CHECK(SbProbe()); + SbUartInit(); + SbSleep(true); + CHECK(efr & 0x10); + CHECK(regs[SB_IER] == 0x10); + CHECK(lcr == 0x03); + SbSleep(false); + CHECK(regs[SB_IER] == 0); +} + +int main(void) +{ + test_probe(); + test_uart_init(); + test_registers(); + test_fifos(); + test_sleep(); + if (failures) { + printf("%d failure(s)\n", failures); + return 1; + } + printf("sblora: all tests passed\n"); + return 0; +}