Skip to content

Commit

Permalink
Impl cpu and mem (#5)
Browse files Browse the repository at this point in the history
* chore: polish code

* feat: impl register and cpu
  • Loading branch information
eigmax authored Oct 26, 2023
1 parent 7760912 commit 22e26d1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/all_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ impl<F: RichField + Extendable<D>, const D: usize> Default for AllStark<F, D> {
}

impl<F: RichField + Extendable<D>, const D: usize> AllStark<F, D> {
pub(crate) fn num_lookups_helper_columns(&self, config: &StarkConfig) -> [usize; 5] {
pub(crate) fn num_lookups_helper_columns(&self, config: &StarkConfig) -> [usize; 6] {
[
// self.arithmetic_stark.num_lookup_helper_columns(config),
self.arithmetic_stark.num_lookup_helper_columns(config),
// self.byte_packing_stark.num_lookup_helper_columns(config),
self.cpu_stark.num_lookup_helper_columns(config),
self.keccak_stark.num_lookup_helper_columns(config),
Expand Down
12 changes: 11 additions & 1 deletion src/cpu/kernel/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use keccak_hash::keccak;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::io::Read;

#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Kernel {
Expand All @@ -15,14 +18,21 @@ pub struct Kernel {
pub(crate) global_labels: HashMap<String, usize>,
}

// FIXME
pub const KERNLE_FILE: &str = "test-vectors/hello";

// FIXME: impl the mips vm
pub(crate) fn combined_kernel() -> Kernel {
let code: Vec<u8> = vec![];
let mut reader = BufReader::new(File::open("test-vectors/hello").unwrap());
let mut code = Vec::new();
reader.read_to_end(&mut code).unwrap();

let code_hash_bytes = keccak(&code).0;
let code_hash_be = core::array::from_fn(|i| {
u32::from_le_bytes(core::array::from_fn(|j| code_hash_bytes[i * 4 + j]))
});
let code_hash = code_hash_be.map(u32::from_be);
log::debug!("code_hash: {:?}", code_hash);

Kernel {
code,
Expand Down
8 changes: 5 additions & 3 deletions src/cpu/kernel/load_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ mod test {
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).unwrap();
let max_mem = 0x40000000;
let p = Program::load_elf(&buffer, max_mem).unwrap();
println!("entry: {}", p.entry);
let _p = Program::load_elf(&buffer, max_mem).unwrap();
/*
log::debug!("entry: {}", p.entry);
p.image.iter().for_each(|(k, v)| {
println!("{}: {}", k, v);
log::debug!("{}: {}", k, v);
})
*/
}
}
10 changes: 9 additions & 1 deletion src/witness/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const KERNEL_CONTEXT: usize = 0;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RegistersState {
pub gprs: [usize; 32],
pub lo: usize,
pub hi: usize,
pub heap: usize,
pub program_counter: usize,
pub is_kernel: bool,
pub context: usize,
Expand All @@ -23,7 +27,11 @@ impl Default for RegistersState {
fn default() -> Self {
Self {
// FIXME: fill in pc
program_counter: 0,
gprs: Default::default(),
lo: 0,
hi: 0,
heap: 0,
program_counter: KERNEL.global_labels["entry"],
is_kernel: true,
context: 0,
}
Expand Down
Binary file added test-vectors/hello
Binary file not shown.

0 comments on commit 22e26d1

Please sign in to comment.