perfect6502_sys/lib.rs
1//! Bindings to perfect6502.
2
3#![no_std]
4
5include!("./bindings.rs");
6
7#[cfg(test)]
8mod tests {
9 use super::*;
10
11 #[test]
12 fn lda_imm() {
13 unsafe {
14 let state = initAndResetChip();
15 assert!(!state.is_null());
16
17 // Write 0x0200 to the reset vector.
18 memory[0xFFFC] = 0x00;
19 memory[0xFFFD] = 0x02;
20
21 // Write LDA #$EF to 0x0200.
22 memory[0x0200] = 0xA9;
23 memory[0x0201] = 0xEF;
24
25 let sp = readSP(state);
26
27 // Execute the 9-cycle reset sequence.
28 for _ in 0..9 {
29 step(state);
30 step(state);
31 }
32
33 // The stack pointer is decremented three times during the reset
34 // sequence.
35 assert_eq!(readSP(state), sp - 3);
36
37 // PC is at the reset vector.
38 assert_eq!(readPC(state), 0x0200);
39
40 let x = readX(state);
41 let y = readY(state);
42
43 // Execute the first cycle of LDA #$EF.
44 step(state);
45 step(state);
46
47 assert_eq!(readIR(state), 0xA9);
48 assert_eq!(readRW(state), 1);
49 assert_eq!(readAddressBus(state), 0x201);
50 assert_eq!(readDataBus(state), 0xEF);
51
52 // Execute the second cycle of LDA #$EF.
53 step(state);
54 step(state);
55
56 assert_eq!(readA(state), 0xEF);
57
58 // X and Y are unchanged.
59 assert_eq!(readX(state), x);
60 assert_eq!(readY(state), y);
61
62 // The Zero flag is clear.
63 assert_eq!(readP(state) & 0x02, 0);
64
65 // There was 22 half cycles, i.e., we called step 22 times.
66 assert_eq!(cycle, 22);
67
68 destroyChip(state);
69 }
70 }
71}