Contur 2
Educational OS kernel simulator
Loading...
Searching...
No Matches
program_builder.h
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <cstdint>
10
11#include "contur/core/types.h"
12
13#include "contur/arch/block.h"
16
17namespace contur {
18
23 [[nodiscard]] inline Block movImm(std::uint8_t reg, RegisterValue value) noexcept
24 {
25 return Block{Instruction::Mov, reg, value, 0};
26 }
27
32 [[nodiscard]] inline Block movReg(std::uint8_t dst, std::uint8_t src) noexcept
33 {
34 return Block{Instruction::Mov, dst, static_cast<RegisterValue>(src), 1};
35 }
36
41 [[nodiscard]] inline Block addImm(std::uint8_t reg, RegisterValue value) noexcept
42 {
43 return Block{Instruction::Add, reg, value, 0};
44 }
45
50 [[nodiscard]] inline Block addReg(std::uint8_t dst, std::uint8_t src) noexcept
51 {
52 return Block{Instruction::Add, dst, static_cast<RegisterValue>(src), 1};
53 }
54
59 [[nodiscard]] inline Block subImm(std::uint8_t reg, RegisterValue value) noexcept
60 {
61 return Block{Instruction::Sub, reg, value, 0};
62 }
63
68 [[nodiscard]] inline Block subReg(std::uint8_t dst, std::uint8_t src) noexcept
69 {
70 return Block{Instruction::Sub, dst, static_cast<RegisterValue>(src), 1};
71 }
72
77 [[nodiscard]] inline Block mulImm(std::uint8_t reg, RegisterValue value) noexcept
78 {
79 return Block{Instruction::Mul, reg, value, 0};
80 }
81
86 [[nodiscard]] inline Block mulReg(std::uint8_t dst, std::uint8_t src) noexcept
87 {
88 return Block{Instruction::Mul, dst, static_cast<RegisterValue>(src), 1};
89 }
90
95 [[nodiscard]] inline Block divImm(std::uint8_t reg, RegisterValue value) noexcept
96 {
97 return Block{Instruction::Div, reg, value, 0};
98 }
99
104 [[nodiscard]] inline Block divReg(std::uint8_t dst, std::uint8_t src) noexcept
105 {
106 return Block{Instruction::Div, dst, static_cast<RegisterValue>(src), 1};
107 }
108
113 [[nodiscard]] inline Block readMem(std::uint8_t reg, MemoryAddress addr) noexcept
114 {
115 return Block{Instruction::ReadMemory, reg, static_cast<RegisterValue>(addr), 0};
116 }
117
122 [[nodiscard]] inline Block writeMem(std::uint8_t reg, MemoryAddress addr) noexcept
123 {
124 return Block{Instruction::WriteMemory, reg, static_cast<RegisterValue>(addr), 0};
125 }
126
131 [[nodiscard]] inline Block compareImm(std::uint8_t reg, RegisterValue value) noexcept
132 {
133 return Block{Instruction::Compare, reg, value, 0};
134 }
135
140 [[nodiscard]] inline Block compareReg(std::uint8_t reg, std::uint8_t other) noexcept
141 {
142 return Block{Instruction::Compare, reg, static_cast<RegisterValue>(other), 1};
143 }
144
148 [[nodiscard]] inline Block jumpEqual(RegisterValue target) noexcept
149 {
150 return Block{Instruction::JumpEqual, 0, target, 0};
151 }
152
156 [[nodiscard]] inline Block jumpNotEqual(RegisterValue target) noexcept
157 {
158 return Block{Instruction::JumpNotEqual, 0, target, 0};
159 }
160
164 [[nodiscard]] inline Block jumpGreater(RegisterValue target) noexcept
165 {
166 return Block{Instruction::JumpGreater, 0, target, 0};
167 }
168
172 [[nodiscard]] inline Block jumpLess(RegisterValue target) noexcept
173 {
174 return Block{Instruction::JumpLess, 0, target, 0};
175 }
176
180 [[nodiscard]] inline Block jumpGreaterEqual(RegisterValue target) noexcept
181 {
182 return Block{Instruction::JumpGreaterEqual, 0, target, 0};
183 }
184
188 [[nodiscard]] inline Block jumpLessEqual(RegisterValue target) noexcept
189 {
190 return Block{Instruction::JumpLessEqual, 0, target, 0};
191 }
192
196 [[nodiscard]] inline Block interrupt(Interrupt code) noexcept
197 {
198 return Block{Instruction::Interrupt, 0, static_cast<RegisterValue>(code), 0};
199 }
200
203 [[nodiscard]] inline Block nop() noexcept
204 {
205 return Block{Instruction::Nop, 0, 0, 0};
206 }
207
210 [[nodiscard]] inline Block halt() noexcept
211 {
212 return Block{Instruction::Halt, 0, 0, 0};
213 }
214
215} // namespace contur
Block — the fundamental unit of simulated memory.
Instruction enum class — all opcodes for the bytecode interpreter.
Interrupt enum class — hardware and software interrupt codes.
Definition block.h:15
Block mulImm(std::uint8_t reg, RegisterValue value) noexcept
Multiplies a register by an immediate value (dst = dst * value).
Interrupt
Interrupt codes used by the CPU and kernel.
Definition interrupt.h:17
Block interrupt(Interrupt code) noexcept
Raises a software interrupt.
Block compareReg(std::uint8_t reg, std::uint8_t other) noexcept
Compares two registers and sets flags.
Block jumpGreater(RegisterValue target) noexcept
Jumps to target if the last compare was greater.
Block jumpNotEqual(RegisterValue target) noexcept
Jumps to target if the last compare was not equal.
Block writeMem(std::uint8_t reg, MemoryAddress addr) noexcept
Writes a register value to a physical memory address.
Block nop() noexcept
No-op — advances the program counter by one.
Block movReg(std::uint8_t dst, std::uint8_t src) noexcept
Copies the value of one register into another.
Block jumpEqual(RegisterValue target) noexcept
Jumps to target if the last compare was equal.
Block divImm(std::uint8_t reg, RegisterValue value) noexcept
Divides a register by an immediate value (dst = dst / value).
Block jumpLessEqual(RegisterValue target) noexcept
Jumps to target if the last compare was less or equal.
@ Mov
Move immediate/register → register.
Definition instruction.h:18
@ Interrupt
Software interrupt (syscall, exit, etc.).
Definition instruction.h:41
@ JumpGreaterEqual
Jump if greater or equal.
Definition instruction.h:33
@ JumpNotEqual
Jump if not equal (ZF clear).
Definition instruction.h:30
@ JumpLess
Jump if less.
Definition instruction.h:32
@ JumpLessEqual
Jump if less or equal.
Definition instruction.h:34
@ Div
Divide: dst = dst / src (may raise DivByZero).
Definition instruction.h:22
@ Halt
Halt execution.
Definition instruction.h:42
@ Mul
Multiply: dst = dst * src.
Definition instruction.h:21
@ ReadMemory
Read value from memory address into register.
Definition instruction.h:39
@ Compare
Compare two registers (sets flags).
Definition instruction.h:28
@ JumpEqual
Jump if equal (ZF set).
Definition instruction.h:29
@ Nop
No operation.
Definition instruction.h:17
@ WriteMemory
Write register value to memory address.
Definition instruction.h:40
@ JumpGreater
Jump if greater.
Definition instruction.h:31
@ Sub
Subtract: dst = dst - src.
Definition instruction.h:20
@ Add
Add: dst = dst + src.
Definition instruction.h:19
Block mulReg(std::uint8_t dst, std::uint8_t src) noexcept
Multiplies a register by the value of another register (dst = dst * src).
Block divReg(std::uint8_t dst, std::uint8_t src) noexcept
Divides a register by the value of another register (dst = dst / src).
Block jumpLess(RegisterValue target) noexcept
Jumps to target if the last compare was less.
Block addReg(std::uint8_t dst, std::uint8_t src) noexcept
Adds the value of one register to another (dst = dst + src).
Block compareImm(std::uint8_t reg, RegisterValue value) noexcept
Compares a register against an immediate value and sets flags.
Block subReg(std::uint8_t dst, std::uint8_t src) noexcept
Subtracts the value of one register from another (dst = dst - src).
Block halt() noexcept
Halts the process unconditionally.
std::uint32_t MemoryAddress
Represents a memory address (physical or virtual).
Definition types.h:15
Block movImm(std::uint8_t reg, RegisterValue value) noexcept
Loads an immediate value into a register.
Block subImm(std::uint8_t reg, RegisterValue value) noexcept
Subtracts an immediate value from a register (dst = dst - value).
Block addImm(std::uint8_t reg, RegisterValue value) noexcept
Adds an immediate value to a register (dst = dst + value).
std::int32_t RegisterValue
Value stored in a CPU register.
Definition types.h:21
Block jumpGreaterEqual(RegisterValue target) noexcept
Jumps to target if the last compare was greater or equal.
Block readMem(std::uint8_t reg, MemoryAddress addr) noexcept
Reads a value from a physical memory address into a register.
A single memory cell in the simulated architecture.
Definition block.h:26
Common type aliases, sentinel constants, and forward declarations used throughout the Contur 2 kernel...