Devices in CREATOR
Devices in CREATOR simulate the behavior of different components that the simulator can interact with, e.g. a terminal, a hard drive, etc.
The code interacts with a device through the program memory, a device specifies a set of "special" memory addresses that can be read and written by both the code and the device.
Device definition
A device is defined by the following:
- An identifier (
id), which uniquely identifies the device. - A control register address (
ctrl_addr), which is typically used by the processor to signal an action to perform on the device. - A status register address (
status_addr), which is typically used by the device to signal its status to the processor. - A data range (
data), which defines a section of memory (startandend) that is shared between the processor and the device, typically for the exchange of data. - A handler function (
handler()), which is called once per cycle, and defines the behavior of the device. - An enabled flag (
enabled), which controls whether the device is enabled or not. If it's not, the callback is not called.
To create a new device, create a new class extending Device and implementing its handler method. Then, instantiate that object and add it to devices with its corresponding ID.
Device handling
All the devices are managed by the DeviceManager, which holds all the devices and acts as an interface with them.
After executing each instruction, the executor calls the handleDevices() method, which executes the handlers for all enabled devices.
Typically, a device reads its control register and checks its value. If it's 0, it exits. If it's not, it works with the other data and clears the control register when it ends by calling reset().
Implemented devices
ConsoleDevice
A device for interacting with CREATOR's console.
Depending on the value stored in the control register (ctrl_addr), it executes one of the following:
1- print int: Reads a word from thedata.startaddress and writes it as an integer value in the console.2- print float: Reads a word from thedata.startaddress and writes it as a float value in the console.3- print double: Reads a word from thedata.startaddress and writes it as a float value in the console.4- print string: Reads the main memory address of a string from thedata.startaddress and writes it in the console.5- read int: Reads an integer from the console and stores it as a word indata.start.6- read float: Reads a float from the console and stores it as a word indata.start.7- read double: Reads a double from the console and stores it as a word indata.start.8- read string: Reads a string from the console of the length specified indata.start + 4and stores it in the main memory address specified indata.start.11- print char: Reads a byte from thedata.startaddress and writes it as a char in the console.12- read char: Reads a char from the console and stores it as a byte indata.start.
OSDriver
A device to simulate system calls to a fictitious "Operating System".
Depending on the value stored in the control register (ctrl_addr), it executes one of the following:
9- sbrk: Allocates a segment of main memory of the size specified indata.startand stores its address indata.start.10- exit: Terminates the current program's execution.