Wired CPU
From GMod Wiki
Wiremod Tool | Back |
Function: | Programmable CPU |
Primary fire: | Creates a CPU/Updates program |
Secondary fire: | No action |
Reload: | No action |
Notes: | Coded by Black Phoenix |
Brief
A programmable CPU
Instructions
Write Assembler program and use Inputs and Outputs to control it
Inputs
- MemBus Link to a hi-speed device that would be attached to top of CPU memory (addresses 65536, 65537, and so on)
- IOBus Link to a data port, or hi-speed device that would be attached to CPU ports space (port0, port1, and so on up to port1023, which is the highest addressable port)
- Frequency Frequency of CPU, default value 2000Hz. Equal to instructions per second
- Clk Clock, if this is equal or greater than 1 then CPU is active
- Reset Resets CPU to initial state. If "use ROM" is checked when compiling then code will also be returned into state after compiling.
- NMI Non-maskable interrupt input. When in protected mode, inputting value in range [32..255] will trigger corresponding interrupt.
Outputs
- Error/Hi-Speed When not in protected mode this output shows last error code, 0 if everything is fine.
Variables
- Code Assembly code, written in ZASM. Also you can use compiled ZC code.
Useful hints
Connect screen to error output if your contraption doesn't work.
Error codes:
0.0 - No error
2.0 - End of program execution/opcode 0
3.0 - Division by zero
4.0 - Unknown opcode
5.0 - Internal error
6.0 - Stack error
7.0 - Memory fault (Read/write violation)
8.0 - External bus error (usually when non-hispeed device is connected to MemBus or IOBus)
9.0 - Page fault (Write access violation)
10.0 - Port Bus fault
11.0 - ROM Error
12.0 - Page error (wrong page id)
13.0 - General Protection Fault
CPU has internal RAM/ROM, which has following parameters: Size - 64KB/65536 values Range - 0..65535
Devices linked to MemBus are connected starting from address 65536. For example for console screen attached to MemBus it would be: Range 1 - 0..65535 (CPU Code & Data) Range 2 - 65536..67584 (Console screen, hi-speed for it is 2048 bytes in size) Reading any value from neither of these ranges can result in error #7.
Reading from negative addresses (-1,-2,etc) redirects read to ports (mov eax,#-1 will read from port0, -2 from port1, etc)
Protected mode is available. While in protected mode CPU no longer signalizes errors using the "Error" output, corresponding interrupts will be called instead, which allows you to handle errors without stopping code.
There are about 100 opcodes available, some of them can be used to make your programs faster. For example, copying data with mcopy operation is faster than same loop in assembly (mcopy is executed instantly). mcopy instruction has limit of max. 32768 bytes per call.
If you want several CPU's to work together you can create some shared memory for all them by attaching their MemBus to same memory unit. Then using "ORG 65536" put compiler into shared area, declare all variables you want, and then go back using "ORG 0" (ORG 65536 and ORG 0 must be before you actual code, for example in DATA area in ZASM).
Example
Original tutorial ZASM asembly programming tutorial
Documentation of high-speed devices
Go here for more Tutorials Wiremod forum: CPU Tutorials Section
In case you don't feel like writing in assembly there is C compiler which turns your code into ZASM assembly.