]> git.sur5r.net Git - cc65/blob - libsrc/common/getcpu.s
add support for detecting 45GS02
[cc65] / libsrc / common / getcpu.s
1 ;
2 ; Ullrich von Bassewitz, 02.04.1999
3 ;
4 ; unsigned char getcpu (void);
5 ;
6
7         .export         _getcpu
8
9 ; ---------------------------------------------------------------------------
10 ; Subroutine to detect an 816. Returns
11 ;
12 ;   - carry clear and 0 in A for a NMOS 6502 CPU
13 ;   - carry set and 1 in A for a 65C02
14 ;   - carry set and 2 in A for a 65816
15 ;   - carry set and 3 in A for a 4510
16 ;   - carry set and 4 in A for a 65SC02
17 ;   - carry set and 5 in A for a 65CE02
18 ;   - carry set and 6 in A for a HuC6280
19 ;   - carry clear and 7 in A for a 2a03/2a07
20 ;   - carry set and 8 in A for a 45GS02
21 ;
22 ; This function uses a $1A opcode which is a INA on the 816 and ignored
23 ; (interpreted as a NOP) on a NMOS 6502. There are several CMOS versions
24 ; of the 6502, but all of them interpret unknown opcodes as NOP so this is
25 ; just what we want.
26
27 .p816                           ; Enable 65816 instructions
28
29 _getcpu:
30
31         lda     #0
32         inc     a               ; .byte $1A ; nop on nmos, inc on every cmos
33         cmp     #1
34         bcc     @IsNMOS
35
36 ; This is at least a 65C02, check for a 65CE02/4510
37
38         .byte   $42,$EA         ; neg on 65CE02/4510, nop #$EA on 65C02, wdm $EA on 65816
39         cmp     #1
40         beq     @HasINCA
41
42 ; This is at least a 65CE02, check for 4510
43
44         lda     #5              ; CPU_65CE02 constant
45         ldx     #0              ; to make sure MAP doesn't do anything, the upper nybl of X and Z must be clear
46         .byte   $5C             ; map on 4510, aug on 65CE02 (acts like 4 byte nop)
47         lda     #3              ; CPU_4510 constant
48         nop
49         cmp     #5
50         beq     @LoadXAndReturn
51
52 ; It is either a 4510 (C65) or a 45GS02 (MEGA65)
53
54         ; 45GS02 supports 32-bit ZP indirect, so use that to check CPU type
55         ; without requiring a functioning MEGA65 hypervisor.
56         ; We setup a read of $200FF, then store a different value in $00FF
57         ; and then re-read $200FF to see if it is unchanged.
58
59         ; Save the 32-bit ZP pointer and data byte
60         ldx     #4
61 @L10:   lda     $fb,x
62         sta     @GetCPUTemp,x
63         dex
64         bpl     @L10
65
66         ; Setup 32-bit pointer to $000200FF
67         lda     #$ff
68         sta     $fb
69         lda     #$00
70         sta     $fc
71         sta     $fe
72         lda     #$02
73         sta     $fd
74
75         ; Prefixing LDA ($nn),Z with a NOP uses 32-bit ZP pointer on 45GS02,
76         ; but normal 16-bit ZP pointer on 4510
77         ; (We assume Z=$00, which will be the normal case)
78         nop                     ; prefix to tell next instruction to be 32-bit ZP
79         .byte   $b2,$fb         ; LDA ($nn),Z
80         eor     #$ff            ; change the value
81         sta     $ff             ; store in $FF
82         ; now try again to load it: If the same, then 45GS02, as $200FF is unchanged
83         nop                     ; prefix to tell next instruction to be 32-bit ZP
84         .byte   $b2,$fb         ; LDA ($nn),Z
85         cmp     $ff             ; does the loaded value match what is in $FF?
86         beq     @Is4510         ; matches, so must be a 4510 = C65
87         bne     @Is45GS02       ; $200FF and $FF have different values, so must be a MEGA65 45GS02
88 @Is4510:
89         jsr     @RestoreGetCPUTemp
90         lda     #3              ; CPU_4510 constant
91         ldx     #0              ; load high byte of word
92         rts
93
94 @Is45GS02:
95         jsr     @RestoreGetCPUTemp
96         lda     #8              ; CPU_45GS02 constant
97         ldx     #0              ; load high byte of word
98         rts
99
100 @RestoreGetCPUTemp:
101         ; Save the 32-bit ZP pointer and data byte
102         ldx     #4
103 @L11:   lda     @GetCPUTemp,x
104         sta     $fb,x
105         dex
106         bpl     @L11
107         rts
108
109         ; Temporary storage for the ZP locations we modify above
110 @GetCPUTemp:    .byte   0,0,0,0,0
111
112
113
114 ; 6502 type of cpu, check for a 2a03/2a07
115 @IsNMOS:
116         sed                     ; set decimal mode, no decimal mode on the 2a03/2a07
117         lda     #9
118         clc
119         adc     #1              ; $01+$09 = $10 on 6502, $01+$09 = $0A on 2a03/2a07
120         cld
121         cmp     #$0a
122         beq     @Is2a03
123         lda     #0              ; CPU_6502 constant
124         beq     @LoadXAndReturn
125 @Is2a03:
126         lda     #7              ; CPU_2A0x constant
127         bne     @LoadXAndReturn
128
129 ; 65C02 cpu type, check for HuC6280
130 @CheckHuC6280:
131         ldx     #6              ; CPU_HUC6280 constant
132         .byte   $22,$EA         ; sax nop on HuC6280 (A=$06, X=$01), nop #$EA on 65C02 (A=$01, X=$06)
133         bne     @LoadXAndReturn
134
135 ; Check for 65816/65802
136 @HasINCA:
137         xba                     ; .byte $EB, put $01 in B accu (nop on 65C02/65SC02)
138         dec     a               ; .byte $3A, A=$00
139         xba                     ; .byte $EB, A=$01 if 65816/65802 and A=$00 if 65C02/65SC02
140         inc     a               ; .byte $1A, A=$02 if 65816/65802 and A=$01 if 65C02/65SC02
141         cmp     #2
142         beq     @LoadXAndReturn
143
144 ; check for 65SC02
145
146         ldy     $F7
147         ldx     #0
148         stx     $F7
149         .byte   $F7,$F7         ; nop nop on 65SC02, smb7 $F7 on 65C02
150         ldx     $F7
151         sty     $F7
152         cpx     #$00
153         bne     @CheckHuC6280
154         lda     #4              ; CPU_65SC02 constant
155 @LoadXAndReturn:
156         ldx     #0
157         rts