Skip to main content

[kernel]

# =============================================================================
# KERNEL (FRAMEWORK FILE) VERSION 2026-04-04
# =============================================================================
# You cannot modify this file. The kernel implements essential operations
# that the compiler depends upon. The funcs marked as "chombit" are
# implemented in a separate file called "kernel.casm". The funcs marked as
# "intrinsic" are generated directly by the compiler.

type char is byte

type string is char[]

# -----------------------------------------------------------------------------
# If a class extends from class_id_base, then you can use class_id() with it.
class class_id_base
end class

# -----------------------------------------------------------------------------
# Array whose elements are 1 byte in size
class array_1
func copy_from(target_index: int, source: array_1, source_index: int,
| num_elements: int)
chombit
end func
end class

# -----------------------------------------------------------------------------
# Array whose elements are 2 bytes in size
class array_2
func copy_from(target_index: int, source: array_2, source_index: int,
| num_elements: int)
chombit
end func
end class

# -----------------------------------------------------------------------------
# Array whose elements are 4 bytes in size
class array_4
func copy_from(target_index: int, source: array_4, source_index: int,
| num_elements: int)
chombit
end func
end class

# -----------------------------------------------------------------------------
# Array whose elements are pointers
class array_p
func copy_from(target_index: int, source: array_p, source_index: int,
| num_elements: int)
chombit
end func
end class

# -----------------------------------------------------------------------------
module math
func abs(x: int): int
intrinsic
end func

func sign(x: int): int
intrinsic
end func

func bit_and(x: int, y: int): int
intrinsic
end func

func bit_or(x: int, y: int): int
intrinsic
end func

func bit_xor(x: int, y: int): int
intrinsic
end func

func bit_not(x: int): int
intrinsic
end func

func shift_left(value: int, shift_amount: int): int
intrinsic
end func

func shift_right_signed(value: int, shift_amount: int): int
intrinsic
end func

func shift_right_unsigned(value: int, shift_amount: int): int
intrinsic
end func
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

# The kernel::scratch_bytes array is a general purpose buffer. Any function
# may use it, but should assume that other function calls will overwrite it.
inset scratch_bytes: char[size 32] located at $d0_00a0 # ..$d0_00bf

func trace_num(id: byte)
intrinsic
end func

func trace(message: string)
intrinsic
end func

func fail(message: string)
intrinsic
end func

func sleep()
intrinsic
end func

# Copies the specified number of bytes from source to dest.
# It is okay for the memory ranges to overlap.
func copy_memory_bytes(target: int, source: int, num_bytes: int)
chombit
end func

func set_memory_bytes(target: int, value: byte, num_bytes: int)
chombit
end func

func set_memory_pairs(target: int, value: pair, num_pairs: int)
chombit
end func

func compare_strings(a: char[], a_size: int, b: char[], b_size: int): int
chombit
end func

func index_of_string(s: char[], s_size: int, pattern: char[], pattern_size: int): int
chombit
end func

func collect_garbage()
chombit
end func
end module