BETRIC, a PHP Virtual Computer

Briefly Exists To Run Instruction Code

Table of Contents

What Was BETRIC?

BETRIC was a virtual computer I wrote in PHP back in 2004. It had an 80 column, 20 line color display (rendered in a <pre> tag in HTML). It had 1024 bytes of virtual RAM. It's virtual processor understood a set of sixteen machine language instructions had seven one-byte registers. Two registers, BX and EX were composed of an H and L (high and low) registers to form 16-bit (word) combined registers for addressing:

RegisterPurpose
AAccumulator for math, interrupt calls, etc.
BHBase pointer for addresses; High portion
BLSame as above; Low portion
CCounter, used for loops and shifts
DData, general purpose
EHExecution, address pointer for running code; High portion
ELSame as above; Low portion

I built BETRIC as a fun exercise. It was completely made up, but modeled most closely to the only experience I'd previously had with low-level programming: the x86 family of processors in the DOS environment.

Screenshot of the BETRIC welcome screen

The friendly and colorful BETRIC welcome screen

I still have all of BETRIC's source (20kb for the computer and 4kb for the assembler, which was still a separate program when I stopped working on it) and it still runs just fine. I used to have it up and running on my website, but since I'd never really found the time to write a tutorial of any sort, I don't believe anyone ever really played with it. If anyone is interested in learning more about BETRIC or using it for any purpose, feel free to contact me.

You can check out the Command and Machine Language references below where you can find more screenshots of BETRIC in action.


The BETRIC Command Reference

d - Dump memory

Shows a "dump" of the memory specified at an (optional) address. If no address is specified, the dump begins at address 0000. The dump consists of three columns. The first is the memory address for the starting byte of each row of data. The second is the actual contents of the binary memory, displayed in hexadecimal format. The third is an ASCII translation of each byte shown in the second column.

d
will dump starting at the default address 0000.
d 23A
will dump starting at address 023A.
Screenshot of BETRIC taking a dump

BETRIC taking a dump

p - Put into memory

The "put" command takes two parameters. The first is the memory address to which you wish to write data. The second is one or more bytes of data, expressed as 8-bit hexadecimal pairs separated by spaces.

p F0 34
puts the single byte value 34 hex (00110100 binary) into memory location 00F0.
p 0 62 3A F1
puts the values '62', '3A', and 'F1' into memory locations 0000, 0001, and 0002.
Screenshot of BETRIC getting input

BETRIC getting input in the form of hexadecimal bytes

u - Unassemble a program

The disassembly command shows any program, starting at address 0000, disassembled to assembly language. This can be helpful for checking or debugging your programs. The output is in three columns. The first is the memory address for the starting byte of each row of shown memory. The second is the actual BETRIC machine language contents of the memory, displayed in hexadecimal format. The third is the BETRIC assembly language translation of the machine language.

u
shows disassembly for program starting at address 0000.
Screenshot of BETRIC unassembling

BETRIC showing a dissasembly (or unassembly) of the current program

r - View/modify registers

The register command allows you to either view the contents of the registers of the BETRIC virtual computer, or change and view them. To view the contents of the registers, simply use the r command with no parameters. If you wish to modify the contents of a register, give the name of the register as the first command, and the hexadecimal byte value you wish to assign to that register as the second.

r
views the current contents of the registers without changing them.
r a ff
puts the hexadecimal value 'ff' in register A
Screenshot of BETRIC show its registers

BETRIC's registers

go - Run a program

The go command starts a program starting at memory address 0000. All programs to by run by BETRIC must be entered so that they begin at address 0000.

go
runs a program at address 0000.
Screenshot of BETRIC running a hello world program

BETRIC running a colorful Hello World program

putchr - Display a character or string

A command not associated with programming in any way, you can direct BETRIC to display a character or string at a particular location on the screen.

putchr Hello Human. 23 2 7
Will display the string "Hello Human." in column 23, on row 2, in color 7.

colors - Displays a color table

The color table shows two rows. The first row shows the color number, the second shows the color itself.

colors

The BETRIC Machine and Assembly Language Reference

All instructions are categorized by function and sorted in numerical order. The machine code instruction in hexadecimal is listed first, followed by the mnemonic assembly language instruction.

Program Flow Instructions

00 - HALT

This instruction will stop all program flow and return control to the BETRIC ROM operating system.

Memory and Copy Instructions

01 - MVALMEM

Move value to memory. This instruction will move the next byte of memory to the RAM memory that is addressed in registers BH and BL. For example, if BH = 00 and BL = 20, the machine code '01FF' will copy the value 'FF' to the RAM memory at the hexadecimal address '20'.

02 - MVALA

Move value to the A register. This instruction will move the next byte of memory to register A. For example, '02FF' will move the value 'FF' to A.

03 - MVALBH

Move value to the BH register. This instruction will move the next byte of memory to register BH. For example, '03FF' will move the value 'FF' to BH.

04 - MVALBL

Move value to the BL register. This instruction will move the next byte of memory to register BL. For example, '04FF' will move the value 'FF' to BL.

05 - MVALC

Move value to the C register. This instruction will move the next byte of memory to register C. For example, '05FF' will move the value 'FF' to C.

06 - MVALD

Move value to the D register. This instruction will move the next byte of memory to register D. For example, '06FF' will move the value 'FF' to D.

0F - MTOMEM

Move from a register to RAM memory. This instruction will copy a value from a register to the address specified in registers BH and BL. The source register for the move operation is specified in the byte following the MTOMEM instruction. The following are valid sources:

For example, if BH = 00, BL = 2F, and A = 3C, then the machine code '0F00' will copy the value '3C' from Register A to the RAM memory at address '002F'.

10 - MTOA

Move from a specified source to Register A. This instruction will copy a value to Register A from a register or memory address (specified in registers BH and BL). The source for the move operation is specified in the byte following the MTOA instruction. The following are valid sources:

For example, if Register C contains the value '3A', the machine code '1002' will copy the value '3A' from Register C to Register A.

11 - MTOBH

Move from a specified source to Register BH. This instruction will copy a value to Register BH from a register or memory address (specified in registers BH and BL). The source for the move operation is specified in the byte following the MTOBH instruction. The following are valid sources:

For example, if Register C contains the value '3A', the machine code '1102' will copy the value '3A' from Register C to Register BH.

12 - MTOBL

Move from a specified source to Register BL. This instruction will copy a value to Register BL from a register or memory address (specified in registers BH and BL). The source for the move operation is specified in the byte following the MTOBL instruction. The following are valid sources:

For example, if Register C contains the value 'F2', the machine code '1202' will copy the value 'F2' from Register C to Register BL.

13 - MTOC

Move from a specified source to Register C. This instruction will copy a value to Register C from a register or memory address (specified in registers BH and BL). The source for the move operation is specified in the byte following the MTOC instruction. The following are valid sources:

For example, if Register A contains the value '3A', the machine code '1300' will copy the value '3A' from Register A to Register C.

14 - MTOD

Move from a specified source to Register D. This instruction will copy a value to Register D from a register or memory address (specified in registers BH and BL). The source for the move operation is specified in the byte following the MTOD instruction. The following are valid sources:

For example, if Register C contains the value '3A', the machine code '1403' will copy the value '3A' from Register C to Register D.

Display Instructions

A0 - DCHR

Display ASCII character in Register A on the screen in the row and column specified in registers C and D. For example, given A=54, C=2, and D=3, the instruction 'A0' will cause the character 'T' (ASCII character 54h) to be displayed in column 2h on row 3h.

A1 - PRSTR

Display a null-terminated ASCII character string from memory. The memory location is specified in registers BH and BL. The starting screen row and column for the string specified in registers C and D.

A2 - DCLR

Changes the color of a row and column on the display screen. The color is set in Register A and the row and and column is specified in registers C and D. For example, given A=13, C=17, and D=4, the instruction 'A2' will cause the character on column 17h, row 4h to be changed to color 13h.