Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : add opcodes cli , clv , clc , cld #29

Merged
merged 8 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion circuits/cpu/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
authors = [""]
compiler_version = "0.1"
compiler_version = ">=0.1"

[dependencies]
opcodes = { path = "../opcodes" }
49 changes: 49 additions & 0 deletions circuits/helpers/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fn test_status_to_num() {
assert(num == 0xa6); // 1 0 1 0 0 1 1 0 -> 0 1 1 0 0 1 0 1 reversed
}


pub fn compute_zn_status(number: Field, mut status: Status) -> Status {
// let mut bits = 0 as u8;
if (number == 0) {
Expand Down Expand Up @@ -131,6 +132,7 @@ fn test_compute_sn_non_empty_status() {
}



pub fn wrapping_add_u16(a: Field, b: Field) -> Field {
addressing::wrapping_add_u16(a, b)
}
Expand Down Expand Up @@ -173,6 +175,36 @@ pub fn wrapping_dec_u8(a: Field, b: Field) -> WrapResult {
}


pub fn clear_interrupt_bit(mut status:Status) -> Status{

status.I = 0;

status

}

pub fn clear_overflow_bit(mut status: Status) -> Status {

status.V = 0;

status
}

pub fn clear_carry_bit(mut status:Status) -> Status {

status.C = 0;

status
}

pub fn clear_decimal_bit(mut status:Status) -> Status {

status.D=0;

status
}


pub fn acc(
op_sorted_addr: [Field; 1],
op_sorted_val: [Field; 1],
Expand Down Expand Up @@ -314,6 +346,7 @@ pub fn compute_permutation_9(
op_sorted_op_rw
)
}

pub fn compute_permutation_21(
r: Field,
op_sorted_step: [Field; 21],
Expand All @@ -328,4 +361,20 @@ pub fn compute_permutation_21(
op_sorted_val,
op_sorted_op_rw
)
}

pub fn compute_permutation_7(
r: Field,
op_sorted_step: [Field; 7],
op_sorted_addr: [Field; 7],
op_sorted_val: [Field; 7],
op_sorted_op_rw: [Field; 7]
) -> Field {
permutation::compute_7(
r,
op_sorted_step,
op_sorted_addr,
op_sorted_val,
op_sorted_op_rw
)
}
11 changes: 11 additions & 0 deletions circuits/helpers/src/permutation.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ pub fn compute_9(
) -> Field {
// TODO: implement
0
}

fn compute_7(
r: Field,
op_sorted_step: [Field; 7],
op_sorted_addr: [Field; 7],
op_sorted_val: [Field; 7],
op_sorted_op_rw: [Field; 7]
) -> Field {
// TODO: implement
0
}
147 changes: 118 additions & 29 deletions circuits/instr/clc/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,46 +1,135 @@
use dep::helpers;
use dep::std;

fn check_op(
//PC = 8203
//X = 8201
//Y = 8202
//A = 8200
//SR = 8205
//M = 8204

fn main(
r: Field,
op_sorted_step: [Field; 21],
op_sorted_addr: [Field; 21],
op_sorted_val: [Field; 21],
op_sorted_op_rw: [Field; 21]
) -> Field {
// TODO: implemented mode retrieval here for the opcode read
// we don't generalize this because each opcode will only have a few modes
// and we want to keep the total constraints down rather than needlessly
// checking for other opcodes every time

let mode: Field = 0; //stubbed for now, we assume the mode is retreived in logic above
let mut sub_arr_step: [Field; 7] = [0,0,0,0,0,0,0];
let mut sub_arr_addr: [Field; 7] = [0,0,0,0,0,0,0];
let mut sub_arr_val: [Field; 7] = [0,0,0,0,0,0,0];
let mut sub_arr_op_rw: [Field; 7] = [0,0,0,0,0,0,0];
for i in 0..7 {
sub_arr_step[i] = op_sorted_step[i];
sub_arr_addr[i] = op_sorted_addr[i];
sub_arr_val[i] = op_sorted_val[i];
sub_arr_op_rw[i] = op_sorted_op_rw[i];
op_sorted_step: [Field; 7],
op_sorted_addr: [Field; 7],
op_sorted_val: [Field; 7],
op_sorted_op_rw: [Field; 7]
) -> pub Field {

//check the program counter
assert(op_sorted_addr[0] == 8203);
assert(op_sorted_op_rw[0] == 0);
let pc = op_sorted_val[0];

//check the opcode
assert(op_sorted_val[1] == 24); //0x18
assert(op_sorted_op_rw[1] == 0);

//update the PC
assert(op_sorted_addr[2] == 8203);
assert(op_sorted_op_rw[2] == 1);
assert(op_sorted_val[2] == pc + 1);

let mut sub_arr_addr: [Field; 2] = [0,0];
let mut sub_arr_val: [Field; 2] = [0,0];
let mut sub_arr_op_rw: [Field; 2] = [0,0];
let offset = 3;
for i in 0..2 {
sub_arr_addr[i] = op_sorted_addr[offset + i];
sub_arr_val[i] = op_sorted_val[offset + i];
sub_arr_op_rw[i] = op_sorted_op_rw[offset + i];
}
let address_and_value: [Field; 2] = helpers::get_addressing(
mode,
sub_arr_step,

helpers::imp(
sub_arr_addr,
sub_arr_val,
sub_arr_op_rw
);

// TODO: implement the rest of the op code checks here
//read status register
assert(op_sorted_addr[5] == 8205);
assert(op_sorted_op_rw[5] == 0);
let sr = op_sorted_val[5];

let mut status = helpers::convert_to_status(sr);

//clear the interrupt flag.
status = helpers::clear_carry_bit(status);
let comp_status = helpers::status_to_num(status);

// TODO: handle case of padding
//write on status register
assert(op_sorted_addr[6] == 8205);
assert(op_sorted_op_rw[6] == 1);
assert(op_sorted_val[6] == comp_status);

// Compute permutation and return it
helpers::compute_permutation(
helpers::compute_permutation_7(
r,
op_sorted_step,
op_sorted_addr,
op_sorted_val,
op_sorted_op_rw
)
}
}

#[test]
fn test_0() -> Field {
main(
1,
[343432, 343433, 343434, 343435, 343436, 343437, 343438],
[8203, 79, 8203, 8203, 80, 8205, 8205],
[49231, 24, 49232, 49232, 224, 38, 38],
[0, 0, 1, 0, 0, 0, 1]
)
}
#[test]
fn test_1() -> Field {
main(
1,
[343492, 343493, 343494, 343495, 343496, 343497, 343498],
[8203, 79, 8203, 8203, 80, 8205, 8205],
[49231, 24, 49232, 49232, 224, 1, 0],
[0, 0, 1, 0, 0, 0, 1]
)
}
#[test]
fn test_2() -> Field {
main(
1,
[343552, 343553, 343554, 343555, 343556, 343557, 343558],
[8203, 79, 8203, 8203, 80, 8205, 8205],
[49231, 24, 49232, 49232, 224, 255, 254],
[0, 0, 1, 0, 0, 0, 1]
)
}
#[test]
fn test_3() -> Field {
main(
1,
[343612, 343613, 343614, 343615, 343616, 343617, 343618],
[8203, 79, 8203, 8203, 80, 8205, 8205],
[49231, 24, 49232, 49232, 224, 123, 122],
[0, 0, 1, 0, 0, 0, 1]
)
}
#[test]
fn test_4() -> Field {
main(
1,
[343672, 343673, 343674, 343675, 343676, 343677, 343678],
[8203, 79, 8203, 8203, 80, 8205, 8205],
[49231, 24, 49232, 49232, 224, 0, 0],
[0, 0, 1, 0, 0, 0, 1]
)
}
#[test]
fn test_5() -> Field {
main(
1,
[343732, 343733, 343734, 343735, 343736, 343737, 343738],
[8203, 79, 8203, 8203, 80, 8205, 8205],
[49231, 24, 49232, 49232, 224, 36, 36],
[0, 0, 1, 0, 0, 0, 1]
)
}


Loading