c64_assembler/memory/mod.rs
1pub mod address_mode;
2pub mod define;
3pub mod label;
4pub mod user_count;
5
6/// Memory address
7pub type Address = u16;
8
9/// Trait for instructions that can handle zeropage and regular addresses.
10///
11/// Zeropage are special address mode on the 6502 where less CPU-cycles and
12/// instruction size is used when accessing RAM below 0x0100.
13///
14/// Based on the is_zeropage the caller can decide if it only needs the low byte or
15/// both the low and high byte.
16pub trait ZeroPage {
17 /// Is the address a zeropage address
18 fn is_zeropage(&self) -> bool;
19
20 /// Get the lower byte of the address
21 fn low(&self) -> u8;
22
23 /// Get the higher byte of the address
24 fn high(&self) -> u8;
25}
26
27impl ZeroPage for Address {
28 /// Is the address a zeropage address
29 ///
30 /// ```
31 /// use c64_assembler::memory::ZeroPage;
32 ///
33 /// assert_eq!(true, 0x00FE.is_zeropage());
34 /// assert_eq!(true, 0x00FF.is_zeropage());
35 /// assert_eq!(false, 0x0100.is_zeropage());
36 /// assert_eq!(false, 0x0101.is_zeropage());
37 /// ```
38 fn is_zeropage(&self) -> bool {
39 *self < 0x100
40 }
41
42 /// Get the lower byte of the address
43 ///
44 /// ```
45 /// use c64_assembler::memory::ZeroPage;
46 ///
47 /// assert_eq!(0xFE, 0x00FE.low());
48 /// assert_eq!(0xFF, 0x00FF.low());
49 /// assert_eq!(0x00, 0x0100.low());
50 /// assert_eq!(0x01, 0x0101.low());
51 /// ```
52 fn low(&self) -> u8 {
53 (self & 0xFF) as u8
54 }
55
56 /// Get the higher byte of the address
57 ///
58 /// ```
59 /// use c64_assembler::memory::ZeroPage;
60 ///
61 /// assert_eq!(0x00, 0x00FE.high());
62 /// assert_eq!(0x00, 0x00FF.high());
63 /// assert_eq!(0x01, 0x0100.high());
64 /// assert_eq!(0x01, 0x0101.high());
65 /// ```
66 fn high(&self) -> u8 {
67 (self >> 8) as u8
68 }
69}