* =========================================================================== include CART_POWER_OFF equ 2 * =========================================================================== SIZEKEY EQU 51 BASE_ORG EQU $0200 WORK_ORG EQU $5000 SETCARTBLOCK EQU $FE00 RSA_LOAD EQU $FE4A DISP EQU $FEC1-WORK_ORG * =========================================================================== * === ================================================= * === Zero-Page Variables ================================================= * === ================================================= * =========================================================================== ORG $0000 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * These are the variables as declared by the Mikey ROM code * Note! dest is presumed to be at memory location zero dest DS 2 checksum DS 1 ; checksum used during decryption powerdowncount DS 2 ptr DS 2 num_blocks DS 1 bitcount DS 1 ; temporary counter bytecount DS 1 ; temporary counter temp0 DS 1 acc0 DS 2 ; accumulator pointers acc1 DS 2 input1 DS 2 ; pointer to multiplicand scratch0 DS SIZEKEY ; scratch areas must be in zero page scratch1 DS SIZEKEY scratch2 DS SIZEKEY inputcode DS SIZEKEY encrypted_area EQU inputcode * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * =========================================================================== * =========================================================================== * =========================================================================== * =========================================================================== RUN $fe00 SetCartBlock ; Fill shift register for cartridge (in X, in A) SEC BRA .0 .1 BCC .2 STX IODAT ; Set cartridge output (bit to shift into register) to X CLC .2 INX STX SYSCTL1 .0 LDX #$02 STX SYSCTL1 ; Power on and strobe ROL STZ IODAT ; Zero for bit to shift into register BNE .1 RTS ; Back to decryption routine ; Clear all memory from $0003 to $FFFF to 0x00 ; (except $fff8!) ClearMemory STZ dest+1 LDA #$00 .3 STA (dest),y INY BNE .3 INC dest+1 BNE .3 ; Routine to initialize Mikey InitMikey LDX #MIKEYVALS_COUNT .4 LDA MikeyValues-1,x LDY MikeyOffsets-1,x STA $FD00,y DEX BNE .4 ; Copy 256 bytes from $FEC1 to $5000 copy LDA Decrypt,x STA WORK_ORG,x INX BNE copy ; Start decryption process for first frame STZ ptr ; Destination address of decrypted data (low) LDA #2 STA ptr+1 ; Destination address of decrypted data (high) STZ checksum ; Initialize transition byte LDA #0 ; Prepare for first block JSR SetCartBlock LDA RCART0 ; Read cart (using strobe CART0) CMP #$FB ; First byte has two's complement of number of blocks in first frame BCC retry ; If value is less than #$FB it is not a correct header STA num_blocks ; Save block count STZ GREENF ; GREENF = 0 STZ BLUEREDF ; BLUEREDF = 0 ; Read entire block of encrypted data (reversed) ReadBlock LDX #SIZEKEY-1 ; Each block in a frame is 50 + 1 byte long .5 LDA RCART0 ; Read cart (using strobe CART0) STA inputcode,x ; Store block data in ZP from $AA to $DC DEX BPL .5 ; Next byte ; Decrypt current block ORA inputcode+1 ; Accumulator contains last byte of encrypted data ORA inputcode+2 BEQ retry ; First three bytes should contain at least one non-zero value, otherwise error LDX #$02 ; Start at position 2 .8 LDA inputcode,x ; Read value SBC modulus,x ; Subtract with public part of encryption key DEX BPL .8 ; Next value BCS retry ; If sanity check fails go to error JSR WORK_ORG ; Start decryption (plaintext = encrypted ^ public_exponent % modulus) LDA (acc0) ; Sanity check for contents of decrypted (and still obfuscated) data's first byte CMP #$15 ; Must always be $15 as hardcoded marker byte of start of obfuscated block BNE retry ; On error LDA checksum ; Get transition byte between blocks LDY #SIZEKEY-1 ; Start at end of decrypted data .9 CLC ADC (acc0),y ; A += decrypted data STA (ptr) ; Store at destination address ($0200) and onwards INC ptr ; Update destination DEY ; Next byte BNE .9 ; Until all data in block is done STA checksum ; Remember last byte INC num_blocks ; Next block BNE ReadBlock ; Continue reading next block of data from cart LDY #CART_POWER_OFF STY IODAT ; Set cart address data TAX BNE retry ; On error retry JMP BASE_ORG ; Run whatever is at $0200 ; Retry lots of times retry INC powerdowncount BNE InitSuzy INC powerdowncount+1 BNE InitSuzy STZ SYSCTL1 ; Reset cart address counter .6 BRA .6 ; Give up and loop forever ; Initialize Suzy InitSuzy LDX #SUZYVALS_COUNT-1 .7 LDA SuzyValues,x LDY SuzyOffsets,x STA $FC00,y DEX BPL .7 STZ CPUSLEEP ; Reset CPU bus request flip flop (draw INSERT GAME sprite) STZ SDONEACK ; Clear SDONEACK JMP InitMikey ; Call routine to initialize Mikey Decrypt echo "RAM code: %HDecrypt" RUN WORK_ORG LDA #INSERT_SCB ; SCBNEXT = INSERT_SCB DB