-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Prabhat1308/main
feat: Add implementation of INY and DEY opcode
- Loading branch information
Showing
3 changed files
with
257 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,4 @@ fn check_op( | |
op_sorted_val, | ||
op_sorted_op_rw | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,146 @@ | ||
use dep::helpers; | ||
use dep::std; | ||
|
||
fn check_op( | ||
|
||
//PC = 8203 | ||
//X = 8202 | ||
//Y = 8202 | ||
//A = 8200 | ||
//SR = 8205 | ||
|
||
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; 9], | ||
op_sorted_addr: [Field; 9], | ||
op_sorted_val: [Field; 9], | ||
op_sorted_op_rw: [Field; 9] | ||
) -> 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] == 136); //0x88 | ||
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 | ||
); | ||
|
||
// read y | ||
assert(op_sorted_addr[5] == 8202); | ||
assert(op_sorted_op_rw[5] == 0); | ||
let value = op_sorted_val[5]; | ||
|
||
// write y | ||
assert(op_sorted_addr[6] == 8202); | ||
assert(op_sorted_op_rw[6] == 1); | ||
let wadd = helpers::wrapping_dec_u8(value, 1); | ||
assert(op_sorted_val[6] == wadd.value); | ||
|
||
// TODO: implement the rest of the op code checks here | ||
//read staus register | ||
assert(op_sorted_addr[7] == 8205); | ||
assert(op_sorted_op_rw[7] == 0); | ||
let sr = op_sorted_val[7]; | ||
|
||
let mut status = helpers::convert_to_status(sr); | ||
|
||
//compute zero and negative flags | ||
status = helpers::compute_zn_status(wadd.value, status); | ||
let comp_status = helpers::status_to_num(status); | ||
|
||
// TODO: handle case of padding | ||
//write on status register | ||
assert(op_sorted_addr[8] == 8205); | ||
assert(op_sorted_op_rw[8] == 1); | ||
assert(op_sorted_val[8] == comp_status); | ||
|
||
// Compute permutation and return it | ||
helpers::compute_permutation( | ||
helpers::compute_permutation_9( | ||
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, 343439, 343440], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 136, 49232, 49232, 224, 127, 126, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_1() -> Field { | ||
main( | ||
1, | ||
[343492, 343493, 343494, 343495, 343496, 343497, 343498, 343499, 343500], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 136, 49232, 49232, 224, 126, 125, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_2() -> Field { | ||
main( | ||
1, | ||
[343552, 343553, 343554, 343555, 343556, 343557, 343558, 343559, 343560], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 136, 49232, 49232, 224, 125, 124, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_3() -> Field { | ||
main( | ||
1, | ||
[343612, 343613, 343614, 343615, 343616, 343617, 343618, 343619, 343620], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 136, 49232, 49232, 224, 124, 123, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_4() -> Field { | ||
main( | ||
1, | ||
[343672, 343673, 343674, 343675, 343676, 343677, 343678, 343679, 343680], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 136, 49232, 49232, 224, 123, 122, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_5() -> Field { | ||
main( | ||
1, | ||
[343732, 343733, 343734, 343735, 343736, 343737, 343738, 343739, 343740], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 136, 49232, 49232, 224, 122, 121, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,144 @@ | ||
use dep::helpers; | ||
use dep::std; | ||
|
||
fn check_op( | ||
//PC = 8203 | ||
//X = 8201 | ||
//Y = 8202 | ||
//A = 8200 | ||
//SR = 8205 | ||
|
||
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; 9], | ||
op_sorted_addr: [Field; 9], | ||
op_sorted_val: [Field; 9], | ||
op_sorted_op_rw: [Field; 9] | ||
) -> 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] == 200); //0xc8 | ||
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 | ||
); | ||
|
||
// read y | ||
assert(op_sorted_addr[5] == 8202); | ||
assert(op_sorted_op_rw[5] == 0); | ||
let value = op_sorted_val[5]; | ||
|
||
// write y | ||
assert(op_sorted_addr[6] == 8202); | ||
assert(op_sorted_op_rw[6] == 1); | ||
let wadd = helpers::wrapping_add_u8(value, 1); | ||
assert(op_sorted_val[6] == wadd.value); | ||
|
||
// TODO: implement the rest of the op code checks here | ||
//read staus register | ||
assert(op_sorted_addr[7] == 8205); | ||
assert(op_sorted_op_rw[7] == 0); | ||
let sr = op_sorted_val[7]; | ||
|
||
let mut status = helpers::convert_to_status(sr); | ||
|
||
//compute zero and negative flags | ||
status = helpers::compute_zn_status(wadd.value, status); | ||
let comp_status = helpers::status_to_num(status); | ||
|
||
// TODO: handle case of padding | ||
//write on status register | ||
assert(op_sorted_addr[8] == 8205); | ||
assert(op_sorted_op_rw[8] == 1); | ||
assert(op_sorted_val[8] == comp_status); | ||
|
||
// Compute permutation and return it | ||
helpers::compute_permutation( | ||
helpers::compute_permutation_9( | ||
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, 343439, 343440], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 200, 49232, 49232, 224, 0, 1, 38, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_1() -> Field { | ||
main( | ||
1, | ||
[343492, 343493, 343494, 343495, 343496, 343497, 343498, 343499, 343500], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 200, 49232, 49232, 224, 1, 2, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_2() -> Field { | ||
main( | ||
1, | ||
[343552, 343553, 343554, 343555, 343556, 343557, 343558, 343559, 343560], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 200, 49232, 49232, 224, 2, 3, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_3() -> Field { | ||
main( | ||
1, | ||
[343612, 343613, 343614, 343615, 343616, 343617, 343618, 343619, 343620], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 200, 49232, 49232, 224, 3, 4, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_4() -> Field { | ||
main( | ||
1, | ||
[343672, 343673, 343674, 343675, 343676, 343677, 343678, 343679, 343680], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 200, 49232, 49232, 224, 4, 5, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
#[test] | ||
fn test_5() -> Field { | ||
main( | ||
1, | ||
[343732, 343733, 343734, 343735, 343736, 343737, 343738, 343739, 343740], | ||
[8203, 79, 8203, 8203, 80, 8202, 8202, 8205, 8205], | ||
[49231, 200, 49232, 49232, 224, 5, 6, 36, 36], | ||
[0, 0, 1, 0, 0, 0, 1, 0, 1] | ||
) | ||
} | ||
|