velxio/test/test_intel/test_4040
David Montero adc99a8035 test_intel: phase D-3 — 4040 SRC + I/O bus wiring (4004 parity)
Apply the same xact_t pattern from the 4004 (phase D-2) to the 4040,
so SRC and the I/O group (WRM/WMP/WRR/WPM/WR0..3/SBM/RDM/RDR/ADM/
RD0..3) drive or sample the multiplexed nibble bus during X2/X3 with
CM-RAM (or CM-ROM for ROM-port ops) strobed.

The 4040's two CM-ROM lines (selected by rom_bank) and its STP/INT
control flow are unchanged — the bus action is staged at M2 and
acted on at X2/X3, fitting cleanly inside the existing PHASE_X3
control-flow block.

Two new integration tests under "4040 + 4002 RAM integration" mirror
the 4004's: SRC + WMP drives the output port, and SRC + WRM/RDM
round-trips a nibble through 4002 storage.

Total test_intel: 117 passing, 11 todo, 0 failed (was 115).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 03:07:11 +02:00
..
4040.c test_intel: phase D-3 — 4040 SRC + I/O bus wiring (4004 parity) 2026-05-01 03:07:11 +02:00
4040.test.js test_intel: phase D-3 — 4040 SRC + I/O bus wiring (4004 parity) 2026-05-01 03:07:11 +02:00
README.md test_intel: Intel 4004 + 4040 chip implementations 2026-04-29 22:45:34 +02:00

README.md

test_4040 — Intel 4040 as a velxio custom chip

See ../autosearch/02_intel_chips_overview.md for the spec.

Status

Implemented (bus skeleton + STOP/STPA). 2/5 active tests pass; 3 are it.todo deferred. ~250 LOC clean-room in 4040.c compiled to fixtures/4040.wasm.

Validated against Intel MCS-40 User's Manual (Nov 1974) — PDF at autosearch/pdfs/mcs40_users_manual.pdf. Cross-checked control logic against markablov/i40xx (MIT) and Kostu96/K4004 (MIT, ships Busicom 141-PF firmware).

What works:

  • Full 24-pin DIP contract per [M40] pp. 1-5/1-6 — STP, STPA, INT, INTA, CY, dual CMROM (CMROM0/CMROM1), dual standby Vdd1/Vdd2.
  • 8-phase frame inherited from 4004 (binary-compatible, [M40] p. 1-22).
  • STP latched at M2 → STOP FF set at X3 → STPA asserts within ~2 instruction cycles ([M40] p. 1-10). Clock and SYNC continue per manual ("CPU executes NOPs in a loop").
  • INT latched at M2 with EIN active → forced JMS to PC=0x003 at X3, INTA asserts ([M40] p. 1-12). Vector address is fixed; no vector table.
  • Index register file extended to 24 × 4 bits (3 banks × 8); SB0/SB1 bank-select FF in place though not exercised by tests.
  • 7-deep PC stack ([M40] p. 1-12) — replaces 4004's 3-deep stack.

Deferred:

  • 14 new instructions' semantics: HLT, BBS, LCR, OR4/OR5, AN6/AN7, DB0/DB1, SB0/SB1, EIN/DIN, RPM (currently all decoded as NOP).
  • BBS return-from-interrupt behaviour (pop stack, restore SRC, restore bank FF, clear INTA).
  • DB0/DB1 ROM-bank-select with 3-cycle takeover delay.
  • HALT FF semantics (HLT opcode).
  • Interrupt-during-2-byte-instruction edge cases ([M40] open question).

Pin contract (24-pin DIP, real silicon)

The 4040 is a 4004 superset; it keeps every 4004 signal and adds:

  • INT (interrupt request, in)
  • STOP (single-step, in)
  • STOP ACK (out)
  • Additional bank-select / index-pointer lines

Action item: pull the exact 4040 pinout from a datasheet and fill in this table before writing .chip.json. Pin order matters for the on-canvas drag-and-drop appearance.

Pin Name Dir Notes
D0..D3 I/O Same multiplexed nibble bus as 4004
CLK1, CLK2 in
SYNC out
RESET in
TEST in
CM-ROM0..1 out 4040 has two ROM strobes (4004 has one)
CM-RAM0..3 out
INT in New on 4040
STOP in New on 4040
STOP ACK out New on 4040
VDD, VSS power

Implementation plan

  1. Land the 4004 first.
  2. Fork its .c source into 4040.c.
  3. Extend the register file from 16 → 24 4-bit registers.
  4. Extend the PC stack from 3-deep → 7-deep.
  5. Add the new opcodes (interrupt enable/disable, return-from-interrupt, stop, the extra register-pair operations).
  6. Add INT pin watch. On rising edge, push PC and vector to fixed address [verify from datasheet].
  7. Add STOP pin watch. On rising edge, freeze the cycle timer and assert STOP ACK.

Target demo sketch

Same as 4004 (LED blink), then a second sketch that uses INT — an external "button" chip drives INT, the 4040's ISR toggles a different output. Demonstrates the only feature that matters versus the 4004.

Files to create later

  • 4040.chip.json
  • 4040.c
  • sketch_blink.asm
  • sketch_irq.asm