Using Devices - Jimmer1/Chip16 GitHub Wiki
As explained in the Chip16 Architecture, Devices are addons that extend the functionality of the Chip16 system. By implementing an interface using only 4 instructions, Devices can do a variety of exotic things that aren't necessarily possibly in a default Chip16 system.
These 4 instructions are as follows:
Mnemonic - Opcode - Description
- WRB - DXNN - Writes a number of bytes specified by the 8-bit constant NN from the memory pointed to by M to Device X.
- RDB - FXNN - Reads a number of bytes from Device X, specified by the 8-bit constant NN, to the memory pointed to by M.
- DPS - EX00 - Sets the 16-bit pointer of Device X to the value held in register 14.
- DPG - EX01 - Sets r14 to the value of the pointer of Device X.
DPS and DPG have the same effect on all Devices which is to set and get the 16-bit internal pointer.
What happens when a number of bytes are written to a Device is Device specific. I shall outline the effects of each instruction on each of the 3 standard Devices below:
-
ConsoleIO - 16-bit internal pointer is a format code with 0 representing ASCII and 1 representing hexadecimal.
-
- WRB - Prints N bytes to the console in the format of the format code.
-
- RDB - Reads N bytes from the console in the format of the format code.
-
MemoryDevice - 16-bit internal pointer holds the address of memory from which you read bytes and to which you write them.
-
- WRB - Writes N bytes to the Device memory pointed to by the 16-bit internal pointer.
-
- RDB - Reads N bytes from the Device memory pointed to by the 16-bit internal pointer.
-
RomDevice - 16-bit internal pointer functions the same as the MemoryDevice's pointer.
-
- WRB - Does nothing.
-
- RDB - Works the same as the RDB instruction of the MemoryDevice.
I shall use the ConsoleIO Device to demonstrate how these instructions are used, the interface is the same across Devices.
Recall: By default, the ConsoleIO Device is in port 0.
WRB
code = [
0x60, 0x00, # acr r0, 0 # r0 = 0
0x60, 0x00, # acr r0, 0 # r0 = 0
0xA0, 0x00, # smp 0 # M = 0
0xD0, 0x04 # wrb d0, 4 # write(d0, 4)
]
RDB
code = [
0xA0, 0x00, # smp $ # M = 4
0xF0, 0x04, # rdb d0, 4 # read(d0, 4)
]
DPS
code = [
0x6F, 0x01, # acr r15, 0x01 # r15 = 1
0xE0, 0x00, # dps d0 # d0.P = r15
]
DPG
code = [
0xE0, 0x01 # dpg d0 # r15 = d0.P
]
Next: Terminating A Program