@@ -58,7 +58,7 @@ impl Chip8 {
58
58
}
59
59
}
60
60
61
- // file and memory related management
61
+ // file & memory management
62
62
impl Chip8 {
63
63
64
64
pub fn load_font ( & mut self ) -> [ u8 ; RAM_SIZE ] {
@@ -70,43 +70,34 @@ impl Chip8 {
70
70
}
71
71
72
72
pub fn load_rom ( & mut self , filename : & String ) {
73
-
74
- let mut rom_file = File :: open ( filename)
75
- . expect ( "[err] error" ) ;
73
+ const MAX_SIZE_FOR_ROM_DATA : u16 = ( RAM_SIZE - 0x200 ) as u16 ;
76
74
75
+ let mut rom_file = File :: open ( filename) . unwrap ( ) ;
77
76
self . load_font ( ) ;
78
77
79
78
let mut rom_raw_data = Vec :: new ( ) ;
80
79
rom_file. read_to_end ( & mut rom_raw_data) . unwrap ( ) ;
81
80
81
+ if rom_raw_data. len ( ) as u16 > MAX_SIZE_FOR_ROM_DATA {
82
+ panic ! ( "File {} is too big for be loaded into the emulator, exiting..." , filename) ;
83
+ }
84
+
82
85
let last_addr = 0x200 + rom_raw_data. len ( ) as usize ;
83
86
self . memory [ 0x200 ..last_addr] . copy_from_slice ( & rom_raw_data) ;
84
87
}
85
88
}
86
89
87
90
// stack management
88
91
impl Chip8 {
92
+
93
+ // note: there's no need to check for overflow and underflow
94
+ // condition because it is handled in opcode's section
89
95
pub fn stack_push ( & mut self , value : u16 ) {
90
- const OVERFLOW_INDEX : usize = STACK_SIZE + 1 ;
91
-
92
- match self . stack_pointer {
93
- OVERFLOW_INDEX => self . stack_pointer = 0 ,
94
- _ => {
95
- self . stack [ self . stack_pointer ] = value;
96
- self . stack_pointer += 1 ;
97
- } ,
98
- }
96
+ self . stack [ self . stack_pointer ] = value;
97
+ self . stack_pointer += 1 ;
99
98
}
100
99
101
100
pub fn stack_pop ( & mut self ) -> u16 {
102
-
103
- if self . stack_pointer == 0 {
104
- println ! ( "[wrn] stack_pop() tried to pop at index 0" ) ;
105
- println ! ( "[wrn] stack_pointer will reset to 0" ) ;
106
-
107
- self . stack_pointer = 1 ;
108
- }
109
-
110
101
self . stack_pointer -= 1 ;
111
102
self . stack [ self . stack_pointer as usize ]
112
103
}
0 commit comments