mirror of
https://github.com/tonk-labs/dappicom.git
synced 2026-01-10 06:07:59 -05:00
add some opcodes
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use crate::opcodes;
|
||||
use dep::std;
|
||||
|
||||
fn perform_op(
|
||||
opcode: Field,
|
||||
t_index: Field,
|
||||
// 0 1 2 3 4 5 6
|
||||
// Index: a register, x register, y register, program conuter, stack pointer, CPU flags, Clock Cycle
|
||||
state_array: [Field; 7],
|
||||
time_sorted_rw: [Field; 10],
|
||||
time_sorted_addr: [Field; 10],
|
||||
@@ -11,32 +14,48 @@ fn perform_op(
|
||||
) -> ([Field; 7], Field) {
|
||||
//if the program stops (ie. opcode == 0, then we will have padded the rest of the opcodes array with 0s and the program will not take anymore action)
|
||||
let mut tn_index = t_index;
|
||||
let mut new_state = state_array;
|
||||
let mut params = (0,0,0);
|
||||
|
||||
let t = opcode - 0xa5 == 0;
|
||||
|
||||
|
||||
//LDA operation
|
||||
if ( opcode - 0xa9 == 0 ) | ( opcode - 0xa5 == 0 ) | ( opcode - 0xb5 == 0 ) | ( opcode - 0xad == 0 ) | ( opcode - 0xbd == 0 ) | ( opcode - 0xb9 == 0 ) | ( opcode - 0xa1 == 0 ) | ( opcode - 0xb1 == 0 ) {
|
||||
let params: (Field, Field, Field) = opcodes::get_op_parameters(opcode);
|
||||
if ( opcode == 0xa9 ) | ( opcode == 0xa5) | ( opcode == 0xb5) | ( opcode == 0xad) | ( opcode == 0xbd) | ( opcode == 0xb9) | ( opcode == 0xa1) | ( opcode == 0xb1) {
|
||||
params = opcodes::get_op_parameters(opcode);
|
||||
let (address, tnn_index) = get_address(
|
||||
params.2, t_index, state_array,
|
||||
time_sorted_rw, time_sorted_addr, time_sorted_value, time_sorted_clk
|
||||
);
|
||||
tn_index = tnn_index;
|
||||
assert(address != 0);
|
||||
new_state[0] = time_sorted_value[tnn_index];
|
||||
assert(time_sorted_addr[tnn_index] == address);
|
||||
assert(time_sorted_rw[tnn_index] == 0);
|
||||
assert(time_sorted_clk[tnn_index] == state_array[6]);
|
||||
//update flags
|
||||
tn_index = tnn_index + 1;
|
||||
};
|
||||
|
||||
|
||||
//TAX operation
|
||||
if ( opcode == 0xAA) {
|
||||
new_state[0] = new_state[1];
|
||||
//update flags
|
||||
}
|
||||
|
||||
|
||||
// if ( opcode - 0xAA == 0) {
|
||||
// // self.tax())
|
||||
// }
|
||||
// if ( opcode - 0xe8 == 0) {
|
||||
// // self.inx())
|
||||
// }
|
||||
// if ( opcode - 0x00 == 0) {
|
||||
// // return)
|
||||
// }
|
||||
|
||||
// if ( /* CLD */ opcode - 0xd8 ) {
|
||||
// // self.status.remove(CpuFlags::DECIMAL_MODE)) ,
|
||||
// }
|
||||
//INX operation
|
||||
if ( opcode == 0xe8) {
|
||||
new_state[1] = new_state[1] + 1;
|
||||
// wrap if the addition is more than 256
|
||||
if new_state[1] == 256 {
|
||||
new_state[1] = 0;
|
||||
}
|
||||
//update flags
|
||||
}
|
||||
|
||||
///* CLD */
|
||||
if ( opcode == 0xd8 ) {
|
||||
// self.status.remove(CpuFlags::DECIMAL_MODE)) ,
|
||||
}
|
||||
|
||||
// if ( /* CLI */ opcode - 0x58 ) {
|
||||
// // self.status.remove(CpuFlags::INTERRUPT_DISABLE))
|
||||
@@ -46,23 +65,23 @@ fn perform_op(
|
||||
// // self.status.remove(CpuFlags::OVERFLOW))
|
||||
// }
|
||||
|
||||
// if ( /* CLC */ opcode - 0x18 == 0 ) {
|
||||
// if ( /* CLC */ opcode == 0x18) {
|
||||
// // self.clear_carry_flag())
|
||||
// }
|
||||
|
||||
// if ( /* SEC */ opcode - 0x38 == 0 ) {
|
||||
// if ( /* SEC */ opcode == 0x38) {
|
||||
// // self.set_carry_flag())
|
||||
// }
|
||||
|
||||
// if ( /* SEI */ opcode - 0x78 == 0 ) {
|
||||
// if ( /* SEI */ opcode == 0x78) {
|
||||
// // self.status.insert(CpuFlags::INTERRUPT_DISABLE))
|
||||
// }
|
||||
|
||||
// if ( /* SED */ opcode - 0xf8 == 0 ) {
|
||||
// if ( /* SED */ opcode == 0xf8) {
|
||||
// // self.status.insert(CpuFlags::DECIMAL_MODE))
|
||||
// }
|
||||
|
||||
// if ( /* PHA */ opcode - 0x48 == 0 ) {
|
||||
// if ( /* PHA */ opcode == 0x48) {
|
||||
// // self.stack_push(self.register_a))
|
||||
// }
|
||||
|
||||
@@ -77,83 +96,83 @@ fn perform_op(
|
||||
// }
|
||||
|
||||
// /* PLP */
|
||||
// if ( opcode - 0x28 == 0) {
|
||||
// if ( opcode == 0x28) {
|
||||
// // self.plp();
|
||||
// }
|
||||
|
||||
// /* ADC */
|
||||
// if ( opcode - 0x69 == 0 ) | ( opcode - 0x65 == 0 ) | ( opcode - 0x75 == 0 ) | ( opcode - 0x6d == 0 ) | ( opcode - 0x7d == 0 ) | ( opcode - 0x79 == 0 ) | ( opcode - 0x61 == 0 ) | ( opcode - 0x71 == 0 ) {
|
||||
// if ( opcode == 0x69) | ( opcode == 0x65) | ( opcode == 0x75) | ( opcode == 0x6d) | ( opcode == 0x7d) | ( opcode == 0x79) | ( opcode == 0x61) | ( opcode == 0x71) {
|
||||
// // self.adc(&opcode.mode);
|
||||
// }
|
||||
|
||||
// /* SBC */
|
||||
// if ( opcode - 0xe9 == 0 ) | ( opcode - 0xe5 == 0 ) | ( opcode - 0xf5 == 0 ) | ( opcode - 0xed == 0 ) | ( opcode - 0xfd == 0 ) | ( opcode - 0xf9 == 0 ) | ( opcode - 0xe1 == 0 ) | ( opcode - 0xf1 == 0 ) {
|
||||
// if ( opcode == 0xe9) | ( opcode == 0xe5) | ( opcode == 0xf5) | ( opcode == 0xed) | ( opcode == 0xfd) | ( opcode == 0xf9) | ( opcode == 0xe1) | ( opcode == 0xf1) {
|
||||
// // self.sbc(&opcode.mode);
|
||||
// }
|
||||
|
||||
// /* AND */
|
||||
// if ( opcode - 0x29 == 0 ) | ( opcode - 0x25 == 0 ) | ( opcode - 0x35 == 0 ) | ( opcode - 0x2d == 0 ) | ( opcode - 0x3d == 0 ) | ( opcode - 0x39 == 0 ) | ( opcode - 0x21 == 0 ) | ( opcode - 0x31 == 0 ) {
|
||||
// if ( opcode == 0x29) | ( opcode == 0x25) | ( opcode == 0x35) | ( opcode == 0x2d) | ( opcode == 0x3d) | ( opcode == 0x39) | ( opcode == 0x21) | ( opcode == 0x31) {
|
||||
// // self.and(&opcode.mode);
|
||||
// }
|
||||
|
||||
// /* EOR */
|
||||
// if ( opcode - 0x49 == 0 ) | ( opcode - 0x45 == 0 ) | ( opcode - 0x55 == 0 ) | ( opcode - 0x4d == 0 ) | ( opcode - 0x5d == 0 ) | ( opcode - 0x59 == 0 ) | ( opcode - 0x41 == 0 ) | ( opcode - 0x51 == 0 ) {
|
||||
// if ( opcode == 0x49) | ( opcode == 0x45) | ( opcode == 0x55) | ( opcode == 0x4d) | ( opcode == 0x5d) | ( opcode == 0x59) | ( opcode == 0x41) | ( opcode == 0x51) {
|
||||
// // self.eor(&opcode.mode);
|
||||
// }
|
||||
|
||||
// /* ORA */
|
||||
// if ( opcode - 0x09 == 0 ) | ( opcode - 0x05 == 0 ) | ( opcode - 0x15 == 0 ) | ( opcode - 0x0d == 0 ) | ( opcode - 0x1d == 0 ) | ( opcode - 0x19 == 0 ) | ( opcode - 0x01 == 0 ) | ( opcode - 0x11 == 0 ) {
|
||||
// if ( opcode == 0x09) | ( opcode == 0x05) | ( opcode == 0x15) | ( opcode == 0x0d) | ( opcode == 0x1d) | ( opcode == 0x19) | ( opcode == 0x01) | ( opcode == 0x11) {
|
||||
// // self.ora(&opcode.mode);
|
||||
// }
|
||||
|
||||
// if ( /* LSR */ opcode - 0x4a == 0 ) {
|
||||
// if ( /* LSR */ opcode == 0x4a ) {
|
||||
// // self.lsr_accumulator()) ,
|
||||
// }
|
||||
|
||||
// /* LSR */
|
||||
// if ( opcode - 0x46 == 0 ) | ( opcode - 0x56 == 0 ) | ( opcode - 0x4e == 0 ) | ( opcode - 0x5e == 0 ) {
|
||||
// if ( opcode == 0x46) | ( opcode == 0x56) | ( opcode == 0x4e) | ( opcode == 0x5e) {
|
||||
// self.lsr(&opcode.mode);
|
||||
// }
|
||||
|
||||
// if ( /*ASL*/ opcode - 0x0a == 0 ) {
|
||||
// if ( /*ASL*/ opcode == 0x0a) {
|
||||
// // self.asl_accumulator()) ,
|
||||
// }
|
||||
|
||||
// /* ASL */
|
||||
// if ( opcode - 0x06 == 0 ) | ( opcode - 0x16 == 0 ) | ( opcode - 0x0e == 0 ) | ( opcode - 0x1e == 0 ) {
|
||||
// if ( opcode == 0x06) | ( opcode == 0x16) | ( opcode == 0x0e) | ( opcode == 0x1e) {
|
||||
// self.asl(&opcode.mode);
|
||||
// }
|
||||
|
||||
// if ( /*ROL*/ opcode - 0x2a == 0 ) {
|
||||
// if ( /*ROL*/ opcode == 0x2a) {
|
||||
// // self.rol_accumulator()) ,
|
||||
// }
|
||||
|
||||
// /* ROL */
|
||||
// if ( opcode - 0x26 == 0 ) | ( opcode - 0x36 == 0 ) | ( opcode - 0x2e == 0 ) | ( opcode - 0x3e == 0 ) {
|
||||
// if ( opcode == 0x26) | ( opcode == 0x36) | ( opcode == 0x2e) | ( opcode == 0x3e) {
|
||||
// self.rol(&opcode.mode);
|
||||
// }
|
||||
|
||||
// if ( /* ROR */ opcode - 0x6a == 0 ) {
|
||||
// if ( /* ROR */ opcode == 0x6a) {
|
||||
// // self.ror_accumulator()) ,
|
||||
// }
|
||||
|
||||
// /* ROR */
|
||||
// if ( opcode - 0x66 == 0 ) | ( opcode - 0x76 == 0 ) | ( opcode - 0x6e == 0 ) | ( opcode - 0x7e == 0 ) {
|
||||
// if ( opcode == 0x66) | ( opcode == 0x76) | ( opcode == 0x6e) | ( opcode == 0x7e) {
|
||||
// self.ror(&opcode.mode);
|
||||
// }
|
||||
|
||||
// /* INC */
|
||||
// if ( opcode - 0xe6 == 0 ) | ( opcode - 0xf6 == 0 ) | ( opcode - 0xee == 0 ) | ( opcode - 0xfe == 0 ) {
|
||||
// if ( opcode == 0xe6) | ( opcode == 0xf6) | ( opcode == 0xee) | ( opcode == 0xfe) {
|
||||
// self.inc(&opcode.mode);
|
||||
// }
|
||||
|
||||
// /* INY */
|
||||
// if ( opcode - 0xc8 == 0 ) {
|
||||
// if ( opcode == 0xc8) {
|
||||
// // self.iny())
|
||||
// }
|
||||
|
||||
// /* DEC */
|
||||
// if ( opcode - 0xc6 == 0 ) | ( opcode - 0xd6 == 0 ) | ( opcode - 0xce == 0 ) | ( opcode - 0xde == 0 ) {
|
||||
// if ( opcode == 0xc6) | ( opcode == 0xd6) | ( opcode == 0xce) | ( opcode == 0xde) {
|
||||
// self.dec(&opcode.mode);
|
||||
// }
|
||||
|
||||
@@ -168,28 +187,28 @@ fn perform_op(
|
||||
// }
|
||||
|
||||
// /* CMP */
|
||||
// if ( opcode - 0xc9 == 0 ) | ( opcode - 0xc5 == 0 ) | ( opcode - 0xd5 == 0 ) | ( opcode - 0xcd == 0 ) | ( opcode - 0xdd == 0 ) | ( opcode - 0xd9 == 0 ) | ( opcode - 0xc1 == 0 ) | ( opcode - 0xd1 == 0 ) {
|
||||
// if ( opcode == 0xc9) | ( opcode == 0xc5) | ( opcode == 0xd5) | ( opcode == 0xcd) | ( opcode == 0xdd) | ( opcode == 0xd9) | ( opcode == 0xc1) | ( opcode == 0xd1) {
|
||||
// self.compare(&opcode.mode, self.register_a);
|
||||
// }
|
||||
|
||||
// /* CPY */
|
||||
// if ( opcode - 0xc0 == 0 ) | ( opcode - 0xc4 == 0 ) | ( opcode - 0xcc == 0 ) {
|
||||
// if ( opcode == 0xc0) | ( opcode == 0xc4) | ( opcode == 0xcc) {
|
||||
// self.compare(&opcode.mode, self.register_y);
|
||||
// }
|
||||
|
||||
// /* CPX */
|
||||
// if ( opcode - 0xe0 == 0 ) | ( opcode - 0xe4 == 0 ) | ( opcode - 0xec == 0 ) {
|
||||
// if ( opcode == 0xe0) | ( opcode == 0xe4) | ( opcode == 0xec) {
|
||||
// // self.compare(&opcode.mode, self.register_x)) ,
|
||||
// }
|
||||
|
||||
// /* JMP Absolute */
|
||||
// if ( opcode - 0x4c == 0 ) {
|
||||
// if ( opcode == 0x4c) {
|
||||
// // let mem_address = self.mem_read_u16(self.program_counter);
|
||||
// // self.program_counter = mem_address;
|
||||
// }
|
||||
|
||||
// /* JMP Indirect */
|
||||
// if ( opcode - 0x6c == 0 ) {
|
||||
// if ( opcode == 0x6c) {
|
||||
// // let mem_address = self.mem_read_u16(self.program_counter);
|
||||
// // // let indirect_ref = self.mem_read_u16(mem_address);
|
||||
// // //6502 bug mode with with page boundary:
|
||||
@@ -200,7 +219,7 @@ fn perform_op(
|
||||
// // let indirect_ref = if mem_address & opcode - 0x00FF == opcode - 0x00FF {
|
||||
// // let lo = self.mem_read(mem_address);
|
||||
// // let hi = self.mem_read(mem_address & opcode - 0xFF00);
|
||||
// // (hi as u16) << 8 == 0 ) | ( (lo as u16)
|
||||
// // (hi as u16== << 8) | ( (lo as u16)
|
||||
// // } else {
|
||||
// // self.mem_read_u16(mem_address)
|
||||
// // };
|
||||
@@ -270,34 +289,45 @@ fn perform_op(
|
||||
// }
|
||||
|
||||
// /* BIT */
|
||||
// if ( opcode - 0x24 == 0 ) | ( opcode - 0x2c ) {
|
||||
// if ( opcode == 0x24) | ( opcode - 0x2c ) {
|
||||
// self.bit(&opcode.mode);
|
||||
// }
|
||||
|
||||
// /* STA */
|
||||
// if ( opcode - 0x85 == 0 ) | ( opcode - 0x95 == 0 ) | ( opcode - 0x8d == 0 ) | ( opcode - 0x9d == 0 ) | ( opcode - 0x99 == 0 ) | ( opcode - 0x81 == 0 ) | ( opcode - 0x91 ) {
|
||||
// self.sta(&opcode.mode);
|
||||
// }
|
||||
// STA
|
||||
if ( opcode == 0x85) | ( opcode == 0x95) | ( opcode == 0x8d) | ( opcode == 0x9d) | ( opcode == 0x99) | ( opcode == 0x81) | ( opcode == 0x91) {
|
||||
// self.sta(&opcode.mode);
|
||||
assert(opcode != 0);
|
||||
params = opcodes::get_op_parameters(opcode);
|
||||
let (address, tnn_index) = get_address(
|
||||
params.2, t_index, state_array,
|
||||
time_sorted_rw, time_sorted_addr, time_sorted_value, time_sorted_clk
|
||||
);
|
||||
assert(time_sorted_addr[tnn_index] == address);
|
||||
assert(time_sorted_rw[tnn_index] == 1);
|
||||
assert(time_sorted_clk[tnn_index] == state_array[6]);
|
||||
assert(time_sorted_value[tnn_index] == state_array[0]);
|
||||
tn_index = tnn_index + 1;
|
||||
};
|
||||
|
||||
// /* STX */
|
||||
// if ( opcode - 0x86 == 0 ) | ( opcode - 0x96 == 0 ) | ( opcode - 0x8e ) {
|
||||
// if ( opcode == 0x86) | ( opcode == 0x96) | ( opcode - 0x8e ) {
|
||||
// let addr = self.get_operand_address(&opcode.mode);
|
||||
// self.mem_write(addr, self.register_x);
|
||||
// }
|
||||
|
||||
// /* STY */
|
||||
// if ( opcode - 0x84 == 0 ) | ( opcode - 0x94 == 0 ) | ( opcode - 0x8c ) {
|
||||
// if ( opcode == 0x84) | ( opcode == 0x94) | ( opcode - 0x8c ) {
|
||||
// let addr = self.get_operand_address(&opcode.mode);
|
||||
// self.mem_write(addr, self.register_y);
|
||||
// }
|
||||
|
||||
// /* LDX */
|
||||
// if ( opcode - 0xa2 == 0 ) | ( opcode - 0xa6 == 0 ) | ( opcode - 0xb6 == 0 ) | ( opcode - 0xae == 0 ) | ( opcode - 0xbe ) {
|
||||
// if ( opcode == 0xa2) | ( opcode == 0xa6) | ( opcode == 0xb6) | ( opcode == 0xae) | ( opcode - 0xbe ) {
|
||||
// self.ldx(&opcode.mode);
|
||||
// }
|
||||
|
||||
// /* LDY */
|
||||
// if ( opcode - 0xa0 == 0 ) | ( opcode - 0xa4 == 0 ) | ( opcode - 0xb4 == 0 ) | ( opcode - 0xac == 0 ) | ( opcode - 0xbc ) {
|
||||
// if ( opcode == 0xa0) | ( opcode == 0xa4) | ( opcode == 0xb4) | ( opcode == 0xac) | ( opcode - 0xbc ) {
|
||||
// self.ldy(&opcode.mode);
|
||||
// }
|
||||
|
||||
@@ -319,7 +349,7 @@ fn perform_op(
|
||||
// }
|
||||
|
||||
// /* TXA */
|
||||
// if ( opcode - 0x8a == 0 ) {
|
||||
// if ( opcode == 0x8a) {
|
||||
// // self.set_register_a(self.register_x)) ,
|
||||
// }
|
||||
|
||||
@@ -329,12 +359,12 @@ fn perform_op(
|
||||
// }
|
||||
|
||||
// /* TYA */
|
||||
// if ( opcode - 0x98 == 0 ) {
|
||||
// if ( opcode == 0x98) {
|
||||
// // self.set_register_a(self.register_y)) ,
|
||||
// }
|
||||
|
||||
|
||||
let mut new_state = state_array;
|
||||
new_state[6] = new_state[6] + params.1;
|
||||
(new_state, tn_index)
|
||||
}
|
||||
|
||||
@@ -349,7 +379,7 @@ fn get_address(
|
||||
time_sorted_value: [Field; 10],
|
||||
time_sorted_clk: [Field; 10]
|
||||
) -> (Field, Field) {
|
||||
// default behacior when mode == 0
|
||||
// default behavior when mode == 0
|
||||
// immediate means we get the value directly off the program_counter
|
||||
let mut address = state_array[3];
|
||||
|
||||
@@ -357,15 +387,29 @@ fn get_address(
|
||||
let mut tn_index = t_index;
|
||||
|
||||
// zeropage means we read the location in memory from the value of the program counter
|
||||
if mode == 1 {
|
||||
let value = time_sorted_value[t_index];
|
||||
if mode == 1 {
|
||||
assert(time_sorted_rw[t_index] == 0);
|
||||
assert(time_sorted_clk[t_index] == state_array[6]);
|
||||
assert(time_sorted_addr[t_index] == state_array[3]);
|
||||
tn_index = tn_index + 1;
|
||||
address = value;
|
||||
address = time_sorted_value[t_index];
|
||||
};
|
||||
// absolute means we use a different heuristic to read the address given the value of program counter
|
||||
if mode == 2 {
|
||||
// these are the values
|
||||
let lo = time_sorted_value[t_index];
|
||||
let hi = (time_sorted_value[t_index + 1] as u64) << 8;
|
||||
// the address matches that which is in the PC
|
||||
assert(time_sorted_addr[t_index] == state_array[3]);
|
||||
assert(time_sorted_addr[t_index+1] == state_array[3] + 1);
|
||||
// these are reads
|
||||
assert(time_sorted_rw[t_index] == 0);
|
||||
assert(time_sorted_rw[t_index + 1] == 0);
|
||||
assert(time_sorted_clk[t_index] == state_array[6]);
|
||||
assert(time_sorted_clk[t_index+1] == state_array[6]);
|
||||
tn_index = tn_index + 2;
|
||||
address = (hi as Field) + lo;
|
||||
};
|
||||
// zeropage x means we read the value in memory from the value in PC and then add the contents of register X to it and read from that addr
|
||||
// zeropage y means we do the same thing except add from register y
|
||||
// absolute x means we use the read heuristic to do what zero page x does
|
||||
|
||||
@@ -101,7 +101,6 @@ fn main(
|
||||
for i in 0..10 {
|
||||
let opcode = opcodes[i];
|
||||
assert(opcode == time_sorted_value[t_index]);
|
||||
t_index = t_index + 1;
|
||||
|
||||
let (updated_state, next_index) = cpu::perform_op(
|
||||
opcode,
|
||||
|
||||
Reference in New Issue
Block a user