Skip to content

Commit

Permalink
Merge pull request tonk-labs#21 from Prabhat1308/main
Browse files Browse the repository at this point in the history
feat: Add implementation of INY and DEY opcode

changes to dey fix

Signed-off-by: Prabhat1308 <[email protected]>

feat: add opcodes clc cld cli clv

Signed-off-by: Prabhat1308 <[email protected]>

reolve merge conflicts

Signed-off-by: Prabhat1308 <[email protected]>

fix: merge issues

Signed-off-by: Prabhat1308 <[email protected]>
  • Loading branch information
goblinoats authored and Prabhat1308 committed Apr 1, 2024
2 parents bca7731 + 31e2c7f commit 376a157
Show file tree
Hide file tree
Showing 11 changed files with 930 additions and 214 deletions.
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" }
44 changes: 44 additions & 0 deletions circuits/helpers/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,34 @@ 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],
Expand Down Expand Up @@ -328,4 +356,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

0 comments on commit 376a157

Please sign in to comment.