@@ -7,6 +7,7 @@ mod branch_test;
77mod group1_test;
88mod group2_test;
99mod group3_test;
10+ mod other_test;
1011mod op;
1112mod sb1_test;
1213mod sb2_test;
@@ -875,7 +876,7 @@ impl CPU {
875876 fn brk ( & mut self ) {
876877 println ! ( "brk: Initalized" ) ;
877878 println ! ( "brk: pc is {}" , self . pc) ;
878- self . stack_push_u16 ( self . pc + 2 - 1 ) ;
879+ self . stack_push_u16 ( self . pc . wrapping_add ( 2 ) ) ;
879880 self . stack_push ( self . flags . bits ( ) ) ;
880881 self . flags . insert ( CpuFlags :: INTERRUPT_DISABLE ) ;
881882 self . pc = self . mem_read_u16 ( 0xFFFE ) ;
@@ -884,18 +885,27 @@ impl CPU {
884885
885886 fn jsr ( & mut self ) {
886887 // Pushes the 16 bit value after self.pc
887- self . stack_push_u16 ( self . pc + 2 - 1 ) ;
888- self . pc = self . mem_read_u16 ( self . pc ) ;
888+ // Note that self.pc is already on the memory value so we just need to push this part + 1
889+ // Eg. JSR 0xAA 0xBB, we would be pushing the memory address of 0xBB
890+ // When rts is called, pc will add 1 automatically so it returns from the next function
891+ println ! ( "jsr: Initalized! The instruction's address is {:#x}" , self . pc) ;
892+ self . stack_push_u16 ( self . pc . wrapping_add ( 2 ) ) ;
893+ // Need to subtract one at the end as run() will add one automatically
894+ self . pc = self . mem_read_u16 ( self . pc . wrapping_add ( 1 ) ) . wrapping_sub ( 1 ) ;
889895 }
890896
891897 fn rti ( & mut self ) {
898+ // Most likely coming from a BRK(software IRQ)- BRK is treated as a 2 byte instruction with an unused immediate
892899 self . flags = CpuFlags :: from_bits_truncate ( self . stack_pop ( ) ) ;
893900 self . pc = self . stack_pop_u16 ( ) ;
901+ // Need to subtract one pc to balance out with the end of run(), which adds one to pc
902+ self . pc = self . pc . wrapping_sub ( 1 ) ;
894903 }
895904
896905 fn rts ( & mut self ) {
897906 self . pc = self . stack_pop_u16 ( ) ;
898- self . pc += 1 ;
907+ println ! ( "rts: Finished. The pc before finishing run is {:#x}" , self . pc) ;
908+ // self.pc does not need to be added as at the end of run, the pc will be added by 1 automatically
899909 }
900910
901911 fn group_three ( & mut self , aaa : u8 , bbb : u8 , _cc : u8 ) {
0 commit comments