Every instruction cycle is lasting two clock cycles and is divided into two phases:
- Execution
- Write back
During execution, the alu computes the updated pointer position and cell value, or assigns the new values from and to the pins. The branch disables the program counter during this cycle and executes the first step of the branch commands [ and ].
During the second cycle, the write back cycle, the registers are updated, the branch restores the stack and reenables the program counter. When processing an branch-instruction, the jump signal is also set and the stack is updated.
The brainfuck logic consists of three parts
- An array with ~60k byte-sized registers as required by the brainfuck language (actually, twice the required cells)
- A pointer with the size of two bytes to address this array, also required by brainfuck
- The ALU computing the follwing instructions: ><+-,.
The instruction memory can hold up to 256 instructions, which are currently hardcoded in the instruction memory source.
The instructions are direct translations of the 8 brainfuck operands:
Brainfuck | Binary |
---|---|
> | 000 |
< | 001 |
+ | 010 |
- | 011 |
, | 100 |
. | 101 |
[ | 110 |
] | 111 |
Every instruction is calculated in two clock cycles, or one instruction cycle.
The compiler located in the directory bfpcompiler can be used to create the machine code for the different destinations.
Assuming the root directory as current location, use the follwing commands to build the executable:
cd bfpcompiler
make
The executable will be located in the directory bfpcompiler.
The executable can be called as
./bfc [-o filename] [-d device="logisim"] filename.bf
The compiler is proofen to detect any error in an entered program. This is fairly easy, as only loop errors (wrong order or count of brackets) could make a program invalid.
The compiler reacts to the following problems:
Problem | Reaction |
---|---|
Closing bracket outside loop | ERROR |
Loop not closed at the end of a program | ERROR |
Loop opened inside another loop | WARNING |
The last warning occurs, because the logisim implementation of the processor is not capable of processing nested loops. It can be safely ignored on other devices.
The original repository with the most recent code is located at My personal git server. Access can be requested via e-mail. Alternatively, the master branch is mirrored to github here [https://github.com/yannickreiss/brainfuck_processor].