Skip to content

Commit

Permalink
First Keccak precompiled. Binary basic draft. (#76)
Browse files Browse the repository at this point in the history
* Create ZiskInst::op_type()

* First draft of keccak precompiled

* Call opcode_execute from binary basic SM

* Fix shift direction in Emu::step_slice()
  • Loading branch information
fractasy authored Sep 9, 2024
1 parent 7044d6f commit 367e45c
Show file tree
Hide file tree
Showing 19 changed files with 833 additions and 280 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ indexmap = { version = "2.2.6", features = ["serde"] }
json = "0.12.4"
elf = "0.7.4"
riscv = { path = "../riscv" }
tiny-keccak = { version = "2.0.2", features = ["keccak"] }

[features]
default = []
Expand Down
38 changes: 38 additions & 0 deletions core/src/inst_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::{Mem, ROM_ENTRY};

/// ZisK instruction context data container, storing the state of the execution
pub struct InstContext {
pub mem: Mem,
pub a: u64,
pub b: u64,
pub c: u64,
pub flag: bool,
pub sp: u64,
pub pc: u64,
pub step: u64,
pub end: bool,
}

/// RisK instruction context implementation
impl InstContext {
/// RisK instruction context constructor
pub fn new() -> InstContext {
InstContext {
mem: Mem::new(),
a: 0,
b: 0,
c: 0,
flag: false,
sp: 0,
pc: ROM_ENTRY,
step: 0,
end: false,
}
}
}

impl Default for InstContext {
fn default() -> Self {
Self::new()
}
}
6 changes: 6 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
mod elf2rom;
mod inst_context;
mod mem;
mod mem_section;
mod riscv2zisk;
mod utils;
mod zisk_definitions;
Expand All @@ -11,6 +14,9 @@ mod zisk_rom;
mod zv2zisk;

pub use elf2rom::*;
pub use inst_context::*;
pub use mem::*;
pub use mem_section::*;
pub use riscv2zisk::*;
pub use utils::*;
pub use zisk_definitions::*;
Expand Down
8 changes: 4 additions & 4 deletions emulator/src/mem.rs → core/src/mem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use zisk_core::UART_ADDR;
use crate::UART_ADDR;

use crate::MemSection;

Expand Down Expand Up @@ -43,13 +43,13 @@ impl Mem {

// Check the start address is not zero
if start == 0 {
panic!("add_write_section() got invalid start={}", start);
panic!("Mem::add_write_section() got invalid start={}", start);
}

// Check the write section address has been set before this call
if self.write_section.start != 0 {
panic!(
"add_write_section() only one write section allowed, write_section.start={}",
"Mem::add_write_section() only one write section allowed, write_section.start={}",
self.write_section.start
);
}
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Mem {
}) {
&self.read_sections[section]
} else {
panic!("Section not found for addr: {} with width: {}", addr, width);
panic!("Mem::read() section not found for addr: {} with width: {}", addr, width);
};

// Calculate the read position
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions core/src/zisk_inst.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{source_to_str, store_to_str};
use crate::{source_to_str, store_to_str, InstContext};

#[derive(Clone, Debug)]
pub enum ZiskOperationType {
None,
Internal,
Arith,
Binary,
Keccak,
}

/// ZisK instruction defined as a binary operation with 2 results: op(a, b) -> (c, flag)
Expand Down Expand Up @@ -36,7 +37,7 @@ pub struct ZiskInst {
pub jmp_offset2: i64,
pub is_external_op: bool,
pub op: u8,
pub func: fn(u64, u64) -> (u64, bool),
pub func: fn(&mut InstContext) -> (),
pub op_str: &'static str,
pub op_type: ZiskOperationType,
pub verbose: String,
Expand Down Expand Up @@ -70,7 +71,7 @@ impl Default for ZiskInst {
jmp_offset2: 0,
is_external_op: false,
op: 0,
func: |_, _| (0, false),
func: |_| (),
op_str: "",
op_type: ZiskOperationType::None,
verbose: String::new(),
Expand Down
2 changes: 1 addition & 1 deletion core/src/zisk_inst_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl ZiskInstBuilder {
jmp_offset2: 0,
is_external_op: false,
op: 0,
func: |_, _| (0, false),
func: |_| (),
op_str: "",
op_type: ZiskOperationType::None,
verbose: String::new(),
Expand Down
5 changes: 3 additions & 2 deletions core/src/zisk_operation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ZiskOperationType;
use crate::{InstContext, ZiskOperationType};

/// ZisK instruction, containing the opcode and a pointer to the function that implements it
#[derive(Debug, PartialEq, Clone)]
Expand All @@ -13,7 +13,7 @@ pub struct ZiskOperation {
pub c: u8,
/// Operation function f(a,b)->(c,flag), where a, b, and c are 32-bit represented as 64-bit
/// (Goldilocks) and flag is either 0 or 1
pub f: fn(a: u64, b: u64) -> (u64, bool),
pub f: fn(&mut InstContext) -> (),
}

impl ZiskOperation {
Expand All @@ -25,6 +25,7 @@ impl ZiskOperation {
"am32" => ZiskOperationType::Arith,
"b" => ZiskOperationType::Binary,
"be" => ZiskOperationType::Binary,
"k" => ZiskOperationType::Keccak,
_ => panic!("ZiskOperation::op_type() found invalid t={}", self.t),
}
}
Expand Down
Loading

0 comments on commit 367e45c

Please sign in to comment.