Skip to content

Instantly share code, notes, and snippets.

@balt-dev
Last active January 13, 2025 21:15
Show Gist options
  • Save balt-dev/d6577a33b9f9589198251fb154f9cd49 to your computer and use it in GitHub Desktop.
Save balt-dev/d6577a33b9f9589198251fb154f9cd49 to your computer and use it in GitHub Desktop.
Twidl - A 6502-inspired 8-bit microprocessor
Each instruction listed has an 8-bit value. A letter denotes a value used by the instruction.
0000 CCCC - INT - Sends an interrupt of the given 4-bit code if bit 6 of S is set. In a compliant implementation, code 0 must be a noop, and code 15 must jump PROG to the address specified at FFFE and FFFF.
0001 0RRR - TES - Sets bit 0 of S to whether the register is zero, sets bit 7 of S to bit 7 of the register.
0001 1III - CHK - Sets bit 3 of S to the bit of A at the index specified by the 3-bit number in I.
0010 0000 - NAN - Takes the NAND of the U register and the A register, and stores it in the A register.
0010 0001 - IOR - ^ but IOR
0010 0010 - AND - ^ but AND
0010 0011 - NOR - ^ but NOR
0010 0100 - ADD - Adds the value in the U register and the A register, and stores it in the A register. Sets bit 1 if unsigned overflow occurred.
0010 0101 - SUB - ^ but arithmetically negates A first
0010 0110 - NOT - Logically negates the value in the A register and stores it back in that register.
0010 0111 - NEG - Arithmetically negates the value in the A register and stores it back in that register.
0010 1000 - SHL - Shifts the value in the A register left by one bit. Arithmetic shift if bit 5 of S is set, logical otherwise.
0010 1001 - SHR - Shifts the value in the A register right by one bit. Arithmetic shift if bit 5 of S is set, logical otherwise.
0010 1010 - ROL - Rotates the value in the A register left by one bit.
0010 1011 - ROR - Rotates the value in the A register right by one bit.
0010 1100 - INC - Increments the value in the A register. Sets bit 1 if unsigned overflow occurred.
0010 1101 - DEC - ^ but decrements instead
0010 1110 - LOW - Clears the top 4 bits of A.
0010 1111 - HIG - Sets the bottom 4 bits of A to the top 4 bits, then clears the top.
0011 0RRR - PSH - Pushes the value in the given register to the stack.
0011 1RRR - POP - Pops the value in the given register from the stack.
0100 BIII - SET - Uses 3 bits into an index into S, and sets that bit to the final bit.
0101 0RRR - CPY - Stores the value of the given register to the C register.
0101 1RRR - PST - Loads the value of the C register to the given register.
0110 BIII - BRA - Uses I as a index into S, and if B matches that bit, swaps the current program index with the address stored in L and H.
0111 AAAA - RJP - Swaps the low and high bytes of the current program index with the address stored in L and H, plus a signed 8-bit value with the high nibble set to the low nibble of the instruction, and the low nibble set to all 0's.
1VVV VVVV - IMM - Stores a 8-bit value into the C register, with the highest bit determined by bit 4 of S.
Registers:
U - General
A - Used by ALU
X - Stack pointer (the hardware stack is stored as 256 bytes separate from the RAM, and grows downward)
C - Used by CPY and PST
L - Address low byte
H - Address high byte
I - Mapped I/O to RAM using L and H as an address
S - 8 bit bitfield - NIASQUCZ - Zero, Carry, User, Query, Immediate Sign, Arithmetic Shift, Interrupt Enabled, Negative
PROG - Inaccessible by normal means, used with BRA and JMP
All registers start as 0, except PROG, which starts at 0x0200, and S, which starts as 0b01000000.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment