SUBLEQ (SUbtract and Branch if Less than or EQual to zero) is a type of OISC (One Instruction Set Computer) / URISC (Ultimate Reduced Instruction Set Computer) architecture. It has only one instruction of the form:
A B C
This is equivalent to the following pseudocode:
mem[B] = mem[B] - mem[A]
if mem[B] <= 0 then goto C
Compound instructions, such as addition, can be formed from sequences of these simple SUBLEQ instructions. For examples of these, see the source files - most compound instructions include a comment about their function. Indirect addressing (pointers) can be emulated through self-modifying code, where operations are performed on the instructions themselves. See [1],[2],[3],[4],[5] for further information.
This project (download) consists of four main parts:
Schematics for a SUBLEQ-based CPU are provided.
A recent version of Logisim is required to simulate the CPU. Simulation instructions are provided below.
There are three main buses in the CPU:
The CPU has six control signals:
These signals are controlled by a 5-microinstruction microprogram which runs in a continous loop. The signals enabled by each of these microinstructions are:
Input/output is performed through memory-mapped I/O, with the I/O driver mapped to address 0xffffffff (-1). If the A signal is enabled, then reading from this address returns the negation of the next input character. If the A signal is not enabled, then the value 0xffffffff is returned. Writing to this address prints the character obtained by performing a bitwise NOT on the value of the B bus. This behaviour allows the following I/O instructions to be performed:
Jumping to address 0xffffffff (-1) disables the clock signal, effectively halting the CPU.
To simulate the CPU:
make in the project root directory, then right-click the component labeled "RAM" > Load Image... > ../image.hexLibraries for programs written in SUBLEQ assembly, as accepted by sqasm, are available.
They provide several common functions:
Several useful procedures are also available:
Usage information and equivalent C code (where appropriate) is provided in the comments of each of these files.
A menu-driven program demonstrating the above libraries is also provided.
Simply call make run in the project root directory to run the demo program.
A very minimal (only 222 bytes in size) SUBLEQ interpreter written in C is available:
#include<stdio.h>
main(int C,char**A){FILE*F=fopen(A[1],"r");int P=0,_[9999],*i=_;while(fscanf(F,
"%d",i++)>0);while(P>=0){int a=_[P++],b=_[P++],c=_[P++];a<0?_[b]+=getchar():b<0
?printf("%c",_[a]):(_[b]-=_[a])<=0?P=c:0;}}
It has been somewhat obfuscated to reduce the code size, so a more readable version is also provided. It should function similarly to sqrun.
The following terminal demonstrates the JavaScript interpreter running the provided demo program:
Loading...
Loading...
© 2007–2010 David A Roberts. Valid XHTML & CSS. Entries (RSS) & Comments (RSS). Source code.