ckb_vm_definitions/
asm.rs

1use crate::{
2    instructions::Instruction, MEMORY_FRAMES, RISCV_GENERAL_REGISTER_NUMBER, RISCV_MAX_MEMORY,
3    RISCV_PAGES,
4};
5
6// The number of trace items to keep
7pub const TRACE_SIZE: usize = 8192;
8pub const TRACE_ITEM_LENGTH: usize = 16;
9
10pub const RET_DECODE_TRACE: u8 = 1;
11pub const RET_ECALL: u8 = 2;
12pub const RET_EBREAK: u8 = 3;
13pub const RET_DYNAMIC_JUMP: u8 = 4;
14pub const RET_MAX_CYCLES_EXCEEDED: u8 = 5;
15pub const RET_CYCLES_OVERFLOW: u8 = 6;
16pub const RET_OUT_OF_BOUND: u8 = 7;
17pub const RET_INVALID_PERMISSION: u8 = 8;
18pub const RET_SLOWPATH: u8 = 9;
19pub const RET_PAUSE: u8 = 10;
20
21#[inline(always)]
22pub fn calculate_slot(addr: u64) -> usize {
23    (addr as usize >> 2) & (TRACE_SIZE - 1)
24}
25
26#[derive(Default)]
27#[repr(C)]
28pub struct Trace {
29    pub address: u64,
30    pub length: u8,
31    pub cycles: u64,
32    pub instructions: [Instruction; TRACE_ITEM_LENGTH + 1],
33    // We are using direct threaded code here:
34    // https://en.wikipedia.org/wiki/Threaded_code
35    pub thread: [u64; TRACE_ITEM_LENGTH + 1],
36}
37
38// Although the memory here is an array, but when it is created,
39//  its size is allocated through memory_size, and its maximum length RISCV_MAX_MEMORY
40//  is used in the structure declaration.
41#[repr(C)]
42pub struct AsmCoreMachine {
43    pub registers: [u64; RISCV_GENERAL_REGISTER_NUMBER],
44    pub pc: u64,
45    pub next_pc: u64,
46    pub running: u8,
47    pub cycles: u64,
48    pub max_cycles: u64,
49    pub chaos_mode: u8,
50    pub chaos_seed: u32,
51    pub load_reservation_address: u64,
52    pub reset_signal: u8,
53    pub isa: u8,
54    pub version: u32,
55
56    pub memory_size: u64,
57    pub frames_size: u64,
58    pub flags_size: u64,
59
60    pub last_read_frame: u64,
61    pub last_write_page: u64,
62
63    pub flags: [u8; RISCV_PAGES],
64    pub frames: [u8; MEMORY_FRAMES],
65    pub traces: [Trace; TRACE_SIZE],
66
67    pub memory: [u8; RISCV_MAX_MEMORY],
68}