CharlesCNorton
commited on
Commit
·
dda1489
1
Parent(s):
90f3f79
Wire all 8 conditional jumps into CPU via imm8[2:0] encoding
Browse files- Expand opcode 0xD to support Z/NZ/C/NC/N/P/V/NV conditions
- Update ref_step() and ThresholdCPU.step() with condition dispatch
- Fix README: tensor count 6296→9429, iron_eval.py→eval.py
- Document Jcc encoding in instruction table
- README.md +5 -5
- threshold_cpu.py +34 -5
README.md
CHANGED
|
@@ -17,8 +17,8 @@ tags:
|
|
| 17 |
Every logic gate is a threshold neuron: `output = 1 if (Σ wᵢxᵢ + b) ≥ 0 else 0`
|
| 18 |
|
| 19 |
```
|
| 20 |
-
Tensors:
|
| 21 |
-
Parameters: 8,
|
| 22 |
```
|
| 23 |
|
| 24 |
---
|
|
@@ -103,7 +103,7 @@ A self-contained, autonomous computational machine:
|
|
| 103 |
| 0xA | LOAD | R[d] = M[addr] |
|
| 104 |
| 0xB | STORE | M[addr] = R[s] |
|
| 105 |
| 0xC | JMP | PC = addr |
|
| 106 |
-
| 0xD |
|
| 107 |
| 0xE | CALL | push PC; PC = addr |
|
| 108 |
| 0xF | HALT | stop execution |
|
| 109 |
|
|
@@ -228,10 +228,10 @@ Interpretation:
|
|
| 228 |
|
| 229 |
## Verification
|
| 230 |
|
| 231 |
-
The model includes `
|
| 232 |
|
| 233 |
```bash
|
| 234 |
-
python
|
| 235 |
# Output: Fitness: 1.000000
|
| 236 |
```
|
| 237 |
|
|
|
|
| 17 |
Every logic gate is a threshold neuron: `output = 1 if (Σ wᵢxᵢ + b) ≥ 0 else 0`
|
| 18 |
|
| 19 |
```
|
| 20 |
+
Tensors: 9,429
|
| 21 |
+
Parameters: 8,286,614
|
| 22 |
```
|
| 23 |
|
| 24 |
---
|
|
|
|
| 103 |
| 0xA | LOAD | R[d] = M[addr] |
|
| 104 |
| 0xB | STORE | M[addr] = R[s] |
|
| 105 |
| 0xC | JMP | PC = addr |
|
| 106 |
+
| 0xD | Jcc | PC = addr if cond (imm8[2:0]: 0=Z,1=NZ,2=C,3=NC,4=N,5=P,6=V,7=NV) |
|
| 107 |
| 0xE | CALL | push PC; PC = addr |
|
| 108 |
| 0xF | HALT | stop execution |
|
| 109 |
|
|
|
|
| 228 |
|
| 229 |
## Verification
|
| 230 |
|
| 231 |
+
The model includes `eval.py` which exhaustively tests all circuits:
|
| 232 |
|
| 233 |
```bash
|
| 234 |
+
python eval.py
|
| 235 |
# Output: Fitness: 1.000000
|
| 236 |
```
|
| 237 |
|
threshold_cpu.py
CHANGED
|
@@ -221,7 +221,24 @@ def ref_step(state: CPUState) -> CPUState:
|
|
| 221 |
s.pc = addr16 & 0xFFFF
|
| 222 |
write_result = False
|
| 223 |
elif opcode == 0xD:
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
s.pc = addr16 & 0xFFFF
|
| 226 |
else:
|
| 227 |
s.pc = next_pc_ext
|
|
@@ -594,17 +611,29 @@ class ThresholdCPU:
|
|
| 594 |
s.pc = addr16 & 0xFFFF
|
| 595 |
write_result = False
|
| 596 |
elif opcode == 0xD:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 597 |
hi_pc = self._conditional_jump_byte(
|
| 598 |
-
|
| 599 |
(next_pc_ext >> 8) & 0xFF,
|
| 600 |
(addr16 >> 8) & 0xFF,
|
| 601 |
-
s.flags[
|
| 602 |
)
|
| 603 |
lo_pc = self._conditional_jump_byte(
|
| 604 |
-
|
| 605 |
next_pc_ext & 0xFF,
|
| 606 |
addr16 & 0xFF,
|
| 607 |
-
s.flags[
|
| 608 |
)
|
| 609 |
s.pc = ((hi_pc & 0xFF) << 8) | (lo_pc & 0xFF)
|
| 610 |
write_result = False
|
|
|
|
| 221 |
s.pc = addr16 & 0xFFFF
|
| 222 |
write_result = False
|
| 223 |
elif opcode == 0xD:
|
| 224 |
+
cond_type = imm8 & 0x7
|
| 225 |
+
if cond_type == 0:
|
| 226 |
+
take_branch = s.flags[0] == 1
|
| 227 |
+
elif cond_type == 1:
|
| 228 |
+
take_branch = s.flags[0] == 0
|
| 229 |
+
elif cond_type == 2:
|
| 230 |
+
take_branch = s.flags[2] == 1
|
| 231 |
+
elif cond_type == 3:
|
| 232 |
+
take_branch = s.flags[2] == 0
|
| 233 |
+
elif cond_type == 4:
|
| 234 |
+
take_branch = s.flags[1] == 1
|
| 235 |
+
elif cond_type == 5:
|
| 236 |
+
take_branch = s.flags[1] == 0
|
| 237 |
+
elif cond_type == 6:
|
| 238 |
+
take_branch = s.flags[3] == 1
|
| 239 |
+
else:
|
| 240 |
+
take_branch = s.flags[3] == 0
|
| 241 |
+
if take_branch:
|
| 242 |
s.pc = addr16 & 0xFFFF
|
| 243 |
else:
|
| 244 |
s.pc = next_pc_ext
|
|
|
|
| 611 |
s.pc = addr16 & 0xFFFF
|
| 612 |
write_result = False
|
| 613 |
elif opcode == 0xD:
|
| 614 |
+
cond_type = imm8 & 0x7
|
| 615 |
+
cond_circuits = [
|
| 616 |
+
("control.jz", 0),
|
| 617 |
+
("control.jnz", 0),
|
| 618 |
+
("control.jc", 2),
|
| 619 |
+
("control.jnc", 2),
|
| 620 |
+
("control.jn", 1),
|
| 621 |
+
("control.jp", 1),
|
| 622 |
+
("control.jv", 3),
|
| 623 |
+
("control.jnv", 3),
|
| 624 |
+
]
|
| 625 |
+
circuit_prefix, flag_idx = cond_circuits[cond_type]
|
| 626 |
hi_pc = self._conditional_jump_byte(
|
| 627 |
+
circuit_prefix,
|
| 628 |
(next_pc_ext >> 8) & 0xFF,
|
| 629 |
(addr16 >> 8) & 0xFF,
|
| 630 |
+
s.flags[flag_idx],
|
| 631 |
)
|
| 632 |
lo_pc = self._conditional_jump_byte(
|
| 633 |
+
circuit_prefix,
|
| 634 |
next_pc_ext & 0xFF,
|
| 635 |
addr16 & 0xFF,
|
| 636 |
+
s.flags[flag_idx],
|
| 637 |
)
|
| 638 |
s.pc = ((hi_pc & 0xFF) << 8) | (lo_pc & 0xFF)
|
| 639 |
write_result = False
|