Skip to content

Instantly share code, notes, and snippets.

@avesus
Created November 19, 2019 15:08
Show Gist options
  • Save avesus/e8661f01cbad7d6154972340d8d3e410 to your computer and use it in GitHub Desktop.
Save avesus/e8661f01cbad7d6154972340d8d3e410 to your computer and use it in GitHub Desktop.
BitBitJump interpreter in 12 bit RAM
# include <stdio.h>
bool memory[12] = {
0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0,
// 0 1 0 1 0 1 0 1 0 1 0 1
// 0 0 1 1 0 0 1 1 0 0 1 1
// 0 0 0 0 1 1 1 1 0 0 0 0
// 0 0 0 0 0 0 0 0 1 1 1 1
};
bool current_instruction[12] = { 0 };
int canonical_address (int b0, int b1, int b2, int b3) {
return (b0 + b1 * 2 + b2 * 4 + b3 * 8) % 12;
}
int canonical_memory () {
return memory[0] * 1 + memory[1] * 2 + memory[2] * 4 + memory[3] * 8 +
memory[4] * 16 + memory[5] * 32 + memory[6] * 64 + memory[7] * 128 +
memory[8] * 256 + memory[9] * 512 + memory[10] * 1024 + memory[11] * 2048;
}
bool bit_n (int val, int pos) {
return (val & (1 << pos)) >> pos;
}
void fill_memory (int program) {
for (int i = 0; i < 12; ++i) {
memory[i] = bit_n(program, i);
}
}
int main () {
for (int program = 1391; program < 4096; ++program) {
fill_memory(program);
if (canonical_memory() != program) {
return -1;
}
int ip = 0;
for (int i = 0; i < 136; ++i) {
// The entire thing is a codeword always.
// Load the instruction:
for (int j = 0; j < 12; ++j) {
current_instruction[j] = memory[(j+ip) % 12];
}
int src = canonical_address(
current_instruction[0], current_instruction[1], current_instruction[2], current_instruction[3]);
int dst = canonical_address(
current_instruction[4], current_instruction[5], current_instruction[6], current_instruction[7]);
int jmpto = canonical_address(
current_instruction[8], current_instruction[9], current_instruction[10], current_instruction[11]);
printf("[%d] %d: mov [%d] <-- [%d] (%d); %s %d\n", canonical_memory(), ip, dst, src, memory[src], jmpto == ip ? "repeat" : "jmp to", jmpto);
memory[dst] = memory[src];
ip = jmpto;
}
printf("\n%d:\n\n", program);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment