Added magic

This commit is contained in:
Erin 2023-08-01 22:13:22 +02:00 committed by ondra05
parent a1efc2dfe4
commit 540555d7a9
4 changed files with 33 additions and 9 deletions

View file

@ -15,25 +15,36 @@ extern crate alloc;
mod macros;
use {alloc::vec::Vec, hashbrown::HashSet};
use {
alloc::{vec, vec::Vec},
hashbrown::HashSet,
};
/// Assembler
///
/// - Opcode-generic, instruction-type-specific methods are named `i_param_<type>`
/// - You likely won't need to use them, but they are here, just in case :)
/// - Instruction-specific methods are named `i_<instruction>`
#[derive(Default)]
pub struct Assembler {
pub buf: Vec<u8>,
pub sub: HashSet<usize>,
}
impl Default for Assembler {
fn default() -> Self {
Self {
buf: vec![0; 3],
sub: Default::default(),
}
}
}
hbbytecode::invoke_with_def!(macros::text::gen_text);
impl Assembler {
hbbytecode::invoke_with_def!(macros::asm::impl_asm);
/// Append 12 zeroes (UN) at the end
/// Append 12 zeroes (UN) at the end and add magic to the begining
///
/// # HoleyBytes lore
///
@ -61,6 +72,7 @@ impl Assembler {
/// Why 12 bytes? That's the size of largest instruction parameter part.
pub fn finalise(&mut self) {
self.buf.extend([0; 12]);
self.buf[0..3].copy_from_slice(&[0xAB, 0x1E, 0x0B]);
}
}

View file

@ -23,6 +23,8 @@ macro_rules! gen_valider {
RegisterArrayOverflow,
/// Program is not validly terminated
InvalidEnd,
/// Program misses magic
MissingMagic
}
/// Error
@ -37,6 +39,14 @@ macro_rules! gen_valider {
/// Perform bytecode validation. If it passes, the program should be
/// sound to execute.
pub fn validate(mut program: &[u8]) -> Result<(), Error> {
// Validate magic
if program.get(0..3) != Some(&[0xAB, 0x1E, 0x0B]) {
return Err(Error {
kind: ErrorKind::MissingMagic,
index: 0,
});
}
// Program has to end with 12 zeroes, if there is less than
// 12 bytes, program is invalid.
if program.len() < 12 {
@ -57,6 +67,7 @@ macro_rules! gen_valider {
}
let start = program;
program = &program[3..];
loop {
use crate::opcode::*;
program = match program {

View file

@ -73,7 +73,7 @@ impl<'a, PfHandler: HandlePageFault, const TIMER_QUOTIENT: usize>
pfhandler: traph,
pc: 0,
program_len: program.len() - 12,
program,
program: &program[3..],
timer: 0,
copier: None,
}

View file

@ -1,6 +1,7 @@
# HoleyBytes ISA Specification
# Bytecode format
- Holey Bytes program should start with following magic: `0xAB1E0B`
- All numbers are encoded little-endian
- There is 256 registers, they are represented by a byte
- Immediate values are 64 bit