Memory segments
The virtual computer's hardware memory is arranged into three memory segments:
| Segment | Addresses | Size |
|---|---|---|
| RAM | $10_0000 … $13_FFFF | 256 kilobytes |
| ROM | $C0_0000 … | up to 1,024 kilobytes (depends on the ROM cartridge size) |
| I/O | $D0_0000 … $D0_4BFF | 19 kilobytes |
The RAM segment
The random access memory (RAM) segment stores data objects that can change over time:
- Kernel memory: The Hybrix framework puts its
CONSOLE_GRIDat the start of the RAM segment. - Dynamic objects: Objects that your program allocates using
NEWare stored in the heap, which is managed by the garbage collector. - Global variables:
MODULEvariables are stored in a special memory block at the start of the heap. - Local variables: Function parameters, local variables, and temporary variables go on the SP stack.
- GC roots: The garbage collector tracks GC root pointers using the Chombit GP stack.
The Hybrix compiler arranges the RAM memory as follows:
| Start Address | Associated variable | Memory block |
|---|---|---|
$10_0000 | KERNEL::CONSOLE_GRID | Hybrix framework console tilemap (4 kilobytes) |
$10_1000 | IO::KERNEL_HEAP_START_ADDRESS | Garbage collector heap (236 kilobytes) |
| The first heap block stores global variables | ||
$13_C000 | IO::KERNEL_STACK_START_ADDRESS | Stack memory (16 kilobytes) |
SP grows forwards from $13_C000 | ||
GP grows backwards from $14_0000 | ||
$14_0000 | IO::KERNEL_STACK_END_ADDRESS IO::RAM_END_ADDRESS | End of RAM memory |
The ROM segment
The read-only memory (ROM) segment stores your compiled code, art resources, and read-only DATA blocks. This memory cannot be modified while the program is running. If your program tries to write to ROM object, an error will occur with a message such as:
CHOMBIT CPU FAULT: CANNOT WRITE TO READ-ONLY ADDRESS C003_CF65
This CPU fault will typically cause your program to terminate immediately.
The ROM segment size depends on the size of the ROM cartridge, which the compiler sets based on the size of your program project. At runtime, the IO::ROM_END_ADDRESS memory location can be used to determine the size.
Mnemonic: The ROM segment addresses begin with
$C. "C" is for code. Your compiled code goes in this segment.
The I/O segment
The input/output segment (I/O segment) is used for interacting with devices using memory-mapped input/output (MMIO). In other words, you control devices and receive inputs by reading and writing to specific locations within this memory segment. The I/O addresses chapter documents these locations, and their definitions can be found in the [IO] framework file.
Most I/O locations are read-only. To help prevent mistakes, a CPU fault error will occur if you try to write to I/O segment bytes that are read-only. IO::BACKGROUND_COLOR is an example of a writeable memory location, whereas IO::CLOCK_SECONDS is an example of a read-only memory location.
Mnemonic: The I/O segment addresses begin with
$D. "D" is for I/O devices.
I/O definitions
MODULE IO
# MEMORY SEGMENT LAYOUT
VAR RAM_END_ADDRESS: INT LOCATED AT $D0_0000 # 001X_XXXX
VAR ROM_END_ADDRESS: INT LOCATED AT $D0_0004 # 00CX_XXXX
VAR IO_END_ADDRESS: INT LOCATED AT $D0_0008 # 00DX_XXXX
. . .
# $D0_0080 - $D0_00BF ARE RESERVED FOR KERNEL GLOBALS
VAR KERNEL_HEAP_START_ADDRESS: INT LOCATED AT $D0_0080
# CONVENTIONALLY, THE HEAP END IS THE STACK START
VAR KERNEL_STACK_START_ADDRESS: INT LOCATED AT $D0_0084
VAR KERNEL_STACK_END_ADDRESS: INT LOCATED AT $D0_0088
. . .
END MODULE
MODULE KERNEL
# THE TEXT CONSOLE USES 8 X 8 TILES, WITH ROOM FOR 40 ROWS AND 28 COLS,
# HOWEVER ITS TILEMAP GRID HAS 64 ROWS X 32 COLS = 2048 PAIRS.
# (THERE ARE 24 UNUSED COLUMNS ON THE RIGHT AND 4 UNUSED ROWS ON THE BOTTOM.)
INSET CONSOLE_GRID: ^PAIR[SIZE 2048] LOCATED AT $10_0000 # ..$10_0FFF
. . .
END MODULE