CPU speed comparison
This article attempts to assess the relative speed of the Game Boy's CPU compared to those of other third-generation video game platforms (Nintendo Entertainment System and Sega Master System).
The speed of a processor depends on both its clock rate and its work per clock. To abstract these, we use a standardized master oscillator at 21.47 MHz, or six times NTSC chroma. This same oscillator was used in the NES and Super NES.
|Nintendo Entertainment System||6502||12||1.79 MHz|
|Super Game Boy||LR35902||20 (M-cycles)||1.07 MHz|
|Sega Master System||Z80||6 (T-states)||3.58 MHz|
To "beat the spread", or complete an operation in fewer master clock cycles than a 6502 does, the LR35902 must complete it in three-fifths of the CPU cycles that the 6502 uses. The LR35902's work per clock is usually superior to 6502 but not always. Because the LR35902 and Z80 are architecturally very similar, both having been derived from the Intel 8080, Z80 results are shown only when the differences cause a noticeable speed discrepancy, such as things that can be done with
(We're not considering Game Boy Color at the moment because it'd always get trounced by the TurboGrafx-16's 7.16 MHz 65C02-based CPU.)
An action game may store data related to the position and behavior of each of several enemies in a struct or record.
For random access to an 8-bit field via a pointer, 6502 wins for one byte, but LR35902 catches up for two consecutive bytes and pulls ahead for more.
; 6502, pointer in zero page ; One byte: 7-8 cycles or 84-96 clocks ; Two random: 14-16 cycles or 168-192 clocks ldy #offsetof(type, fieldname) ; 2 lda ($00),y ; 6, minus 1 for read not crossing page iny ; 2 lda ($00),y ; 6, minus 1 for read not crossing page ; LR35902, pointer in DE ; One byte: 7 cycles or 140 clocks ; Two consecutive: 9 cycles or 180 clocks ld hl,offsetof(type, fieldname) ; 3 add hl,de ; 2 ld a,[hl+] ; 2 ld a,[hl+] ; 2 ; Z80, pointer in IX ; One byte: 19 T-states or 114 clocks ; Two random: 38 T-states or 228 clocks ld a,[ix+offsetof(type, fieldname)] ; 19 ld a,[ix+offsetof(type, fieldname)+1] ; 19
However, the 6502 has a trick up its sleeve: structure of arrays. If records are statically allocated, all the values for one field can be placed together. This lets the 6502 use "absolute indexed" addressing, which adds an offset in an 8-bit register to a 16-bit pointer.
; 6502, SOA index in X ; Each byte: 4-5 cycles or 48-60 clocks lda fieldname,x ; 5, minus 1 for read not crossing page lda otherfieldname,x ; LR35902 or Z80, SOA index in DE ; Each byte (LR35902): 7 cycles or 140 clocks ld hl,fieldname ; 3 add hl,de ; 2 ld a,[hl] ; 2 ld hl,otherfieldname add hl,de ld a,[hl]
But if the positions differ by only one bit, such as if they're 8, 16, 32, or 64 bytes apart, bit operations on L can speed up calculating the address for subsequent accesses. This requires thinking of your record as a binary hypercube, where each field is connected to the fields whose address differs by one bit.
; 6502, SOA index in X ; Each byte: 4-5 cycles or 48-60 clocks ; Two random: 8-10 cycles or 96-120 clocks lda fieldname,x ; 5, minus 1 for read not crossing page lda otherfieldname,x ; LR35902 or Z80, SOA index in DE ; First byte (LR35902): 7 cycles or 140 clocks ; Two bytes, 1 bit different (LR35902): 11 cycles or 220 clocks ld hl,fieldname ; 3 add hl,de ; 2 ld a,[hl] ; 2 set log2(otherfieldname^fieldname),l ; 2 ld a,[hl] ; 2