Game Boy CPU isn't a Z80. What is it?

Discussion of programming and development for the original Game Boy and Game Boy Color.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Game Boy CPU isn't a Z80. What is it?

Post by tepples »

The IC containing the NES CPU is a Ricoh 2A03. It consists of a DMA controller and programmable sound generator wrapped around a barely modified MOS 6502 core. (Before the introduction of NOAC famiclones, it wasn't quite a system on chip because the PPU lay elsewhere.) Programmers are comfortable saying the CPU is a 6502. The Game Boy CPU, on the other hand, uses a special snowflake architecture that's clearly descended from the Intel 8080 but not exactly the same as any other well-known part.

"GBZ80": Fan name for the Game Boy instruction set based on the overall similarity of its unprefixed and $CB-prefixed instructions to those of Zilog's Z80. Invites confusion because the lack of IX/IY register and exchange instructions makes it handle quite unlike a Game Gear's authentic Z80 in practice, particularly when accessing fields of structs.

"LR35902": This originated from a package marking on Game Boy systems using the earliest revision of the processor. Because the die marking was the only part number that looked anything like Sharp's other part numbers, this was popularized by the schematic by Jeff Frohwein. It is believed to refer to the entire system on chip (CPU + memory controller + APU + PPU), just as "2A03" refers to the NES CPU + DMA controller + APU.

"SM83": Someone on the GBDev Discord server claims to have seen datasheets for other Sharp microcontrollers or systems on chip that have the same instruction set as the Game Boy CPU, and they call the core "SM83". I'd like to get the identity of this datasheet into a form that I can cite, as things said only in chat tend to disappear down a memory hole more quickly than things said in a newsgroup post, mailing list post, or other forum post with a URL.
DParrott
Posts: 25
Joined: Thu Jul 29, 2010 4:39 am

Re: Game Boy CPU isn't a Z80. What is it?

Post by DParrott »

I think this is one of the two docs being referred to, see page 140.

http://bitsavers.informatik.uni-stuttga ... a_Book.pdf
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Game Boy CPU isn't a Z80. What is it?

Post by dougeff »

My "Programming the Z80" book was partly unhelpful, because it taught several things using opcodes that just didn't exist on the gameboy.

DJNZ is a standard copy loop. Gameboy doesn't have it.

Index registers for accessing arrays. Gameboy doesn't have them.

The secondary registers, or whatever they are called, AF' BC' DE' HL' don't exist.

And there's a missing ld HL, (nn) opcode (and the reverse) , and a missing NEG opcode. There are slower workarounds for these, but they would have been nice.
nesdoug.com -- blog/tutorial on programming for the NES
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Game Boy CPU isn't a Z80. What is it?

Post by tepples »

Let the record state that the document that DParrott cited is Sharp's Microcomputer Databook (1996). In addition to the SM83 (which is described as having a different SOC wrapped around it), it describes several other systems on chip: one using ARM, one using an (unfortunately undescribed) 16-bit CPU "SM6000", one using a different "SM85" CPU, and a couple 4-bit CPUs. It is one of five volumes in Sharp's integrated circuit documentation series, the others being MOS (discrete logic and ADC/DAC stuff apparently), Memory, Bipolar, and Reliability Handbook.

I'd link another copy on Archive.org, but Archive.org appears down right now.

@dougeff
On Game Boy, DJNZ can be expanded as a pseudoinstruction with encoding $05 $20 according to optables.
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Game Boy CPU isn't a Z80. What is it?

Post by dougeff »

My point is that you can't program something on a true Z80 machine, and then put the same code on a gameboy. Not even close. You'll basically have to rewrite it.

I don't feel like it can be called a Z80 chip.
nesdoug.com -- blog/tutorial on programming for the NES
Shonumi
Posts: 342
Joined: Sun Jan 26, 2014 9:31 am

Re: Game Boy CPU isn't a Z80. What is it?

Post by Shonumi »

"GBZ80": Fan name for the Game Boy instruction set based on the overall similarity of its unprefixed and $CB-prefixed instructions to those of Zilog's Z80. Invites confusion because the lack of IX/IY register and exchange instructions makes it handle quite unlike a Game Gear's authentic Z80 in practice, particularly when accessing fields of structs.
I disagree. If anything, we call it a GBZ80 to both mark the similarities and to highlight the fact that it is very separate from "real" Z80. To me it's always been "The Game Boy's custom take on a Z80", and never once confused it as being a full Z80. When I first started reading up on the Game Boy, nearly every document called it "Z80-like" but not specifically a real Z80, which was enough for even a clueless 20-something year-old like me at the time to understand what kind of message they were sending.

I never use the term "LR35902". It refers to the whole SoC as I see it, and it's not helpful when specifically talking about the CPU. Even if I did use it to refer to the CPU, it's... clunky. Same reason I don't refer to the ARM9 on the NDS as the ARM946E-S; I'm not going to remember a random combination of numbers and letters when there's a shorthand version available that perfectly describes the subject at hand.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Game Boy CPU isn't a Z80. What is it?

Post by tepples »

I've tried to write about the CPU core in the LR35902 SOC generically as an "8080 family CPU" in the sense that the Sony SPC700 is a 6502 family CPU. This works because the biggest things in the 8080 opcode matrix that are missing (not moved) in the "GBZ80" opcode matrix are the following, which I've tended to find less consequential than the omission of IX/IY and the alternate register set:
  • Loading or storing the 16-bit contents of HL at an absolute address ($22, $2A)
  • Jump, call, and return based on bit 7 or parity of the last ALU result
  • Exchange HL with 16-bit word at top of stack ($E3) or with DE ($EB)
User avatar
TmEE
Posts: 960
Joined: Wed Feb 13, 2008 9:10 am
Location: Norway (50 and 60Hz compatible :P)
Contact:

Re: Game Boy CPU isn't a Z80. What is it?

Post by TmEE »

I refer to it as G80.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Game Boy CPU isn't a Z80. What is it?

Post by lidnariq »

One more option, even more pedantically true and useless than LR35902: the die appears to have the marking "CESL2OA" or "CE5L2OA". (see also: gekkio's tweet here )

My personal opinion is that, even though "GBZ80" is a fundamentally flawed code name, it is nonetheless the one that we have a long history of using and is it is unambiguous.
gekkio
Posts: 49
Joined: Fri Oct 16, 2015 6:18 am

Re: Game Boy CPU isn't a Z80. What is it?

Post by gekkio »

Let's take a look at various facts...

SM8311/13/14/15 (Sharp Microcomputer Data Book 1996):

* Sharp SM8311/SM8313/SM8314/SM8315 are 8-bit "microcomputers" intended for home appliances
* They use a SM83 CPU core
* They use a CPU register set and an instruction set that is a 100% match with the Game Boy CPU (none missing, no extras). There are no other known 8-bit CPUs with the exactly same instruction set
* We know that the instructions match, but we don't know if the opcodes are exactly the same
* They use fetch/execute overlap ("pipeline") that matches exactly my observations of DMG B and SGB CPUs
* The pipeline diagram has errors: Instruction N+1 fetches an operand but no opcode, Instruction N+2 fetches two opcodes
* Instruction "type" count is listed as 74 in the overview section, which matches the amount of instructions in the listing later
* Interrupts are similar to GB, but not exactly the same (NMI, amount of interrupts). Note that CGB CPUs have an NMI pin though.... 8-)
* The reset behaviour described in the datasheet matches my observations of DMG B and SGB CPUs:
Upon counting the system clock pulses for the specified period, it exits hardware reset condition and executes the program at address OOOOH
IIRC DMG/SGB count exactly 32768 machine cycles after reset is deasserted before starting the opcode fetch of the first instruction from $0000.

SM8320 (Sharp Microcomputers Data Book 1990):

* Sharp SM8320 is a 8-bit "microcomputer" intended for air conditioners
* It uses an SM83 CPU core
* Instruction set size is listed as 81 on the overview page
* Instruction list looks like a 100% match with SM831x and Game Boy CPU, although this list is more high-level than the SM831x list so we can't be sure
* Instruction list says instruction set size is 77 (not 81 like on the overview page, or 74 like on SM831x)

SM-812/813 (Sharp Semiconductor Data Book, MOS Edition, 1986):

* CPU parts of the block diagram look similar to the SM83 core part in SM8320 block diagram
* CPU registers shown on the block diagram are the same as in SM83 and GB CPU
* Instruction set size is listed as 81

And now, some speculation:

* SM83 and Game Boy CPU seem like a perfect match (unlike for example SM82 vs GB or SM85 vs GB). I think it's reasonable to say at least that GB uses an SM83-compatible core
* SM831x and SM8320 both use SM83 core, but we have three listed instruction set sizes: 74, 77, and 81. I think the instruction set is the same, but the way how "size" is calculated has changed so there's some mistakes and confusion
* SM8320 and SM-812/813 both list instruction size as 81 on the overview page. SM8320 has a different count later, but having the exact same size on the overview page suggests the instruction set *might* be the same.
* -> maybe GB uses the unnamed core, which might be a predecessor of SM83. Even if the instruction set is the same, it's reasonable to believe that this unnamed core is not exactly the same as SM83.

And finally some links:

Some index page that shows various numbering schemes of *publicly available Sharp chips* (includes LR-prefixed chips): https://4donline.ihs.com/images/VipMast ... 19-120.pdf
More details of SM6000, which is not similar to GB: https://4donline.ihs.com/images/VipMast ... 3904-1.pdf
More details of SM85, which is not similar to GB: https://4donline.ihs.com/images/VipMast ... 3906-1.pdf
Mentions 8-bit SH-11 core, which is "low-power Z80": https://4donline.ihs.com/images/VipMast ... 019-18.pdf
8-bit microcomputer list that mentions SM82/83/84/85: https://4donline.ihs.com/images/VipMast ... 019-14.pdf
Another 8-bit microcomputer list: https://4donline.ihs.com/images/VipMast ... 017-27.pdf
Some weird copyright page that lists LR35902 and some SM8x chips. Googling some of these chips is what started this SM8x investigation: http://www.copyrightencyclopedia.com/lr ... 2-lz-2111/
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Game Boy CPU isn't a Z80. What is it?

Post by Pokun »

I don't know, I think it's pretty clear that Nintendo wanted a Z80-like CPU. The Z80 was the most common architecture among game developers at the time, and they even used the superior Z80 mnemonics to make programmers more comfortable.

I copy-paste a lot of code between my Game Boy and MSX programming. Things like loops using DJNZ and such must be manually edited of course, but it's a lot easier than translating 6502 code to Z80.

I guess it's a 8080 family, Z80-like custom microprocessor with possibly a SM83-compatible core.
GBZ80 is fine.
nitro2k01
Posts: 252
Joined: Sat Aug 28, 2010 9:01 am

Re: Game Boy CPU isn't a Z80. What is it?

Post by nitro2k01 »

gekkio wrote:* Interrupts are similar to GB, but not exactly the same (NMI, amount of interrupts). Note that CGB CPUs have an NMI pin though.... 8-)
Have you documented the details of this publicly anywhere?
gekkio
Posts: 49
Joined: Fri Oct 16, 2015 6:18 am

Re: Game Boy CPU isn't a Z80. What is it?

Post by gekkio »

nitro2k01 wrote:Have you documented the details of this publicly anywhere?
Do you mean the NMI stuff? Not yet, because I haven't researched it...I just know there's an NMI pin because I've seen the test point on CGB boards.
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: Game Boy CPU isn't a Z80. What is it?

Post by Oziphantom »

I think dropping the Z is what is needed. its a GB80 over an 8080. I doubt going for a Z80 was keen for Nintendo, they will have gone kooky custom to keep control and stop developers from using it. Its a lot easier to go kooky custom on a 8080 than a 6502. As the 8080 has microcode so you can just modify the microcode to change the instructions around. Maybe the 8080 with its 4 bit ALU uses less power per tick than a 6502? However I would think that the 65C02 of say the Lynx probably gave better power per clock when you factor in the work done by the CPU. I also wonder which year it was exactly that MOS wisened up to Ricoh deception and was starting to hit Nintendo/Ricoh with "you owe us a lot of royalties you thieves" so moving to Sharp and not a MOS design was to safe guard them.
User avatar
ISSOtm
Posts: 58
Joined: Fri Jan 04, 2019 5:31 pm
Location: France, right of a pile of consoles
Contact:

Re: Game Boy CPU isn't a Z80. What is it?

Post by ISSOtm »

The GB CPU is basically known to have a different architecture, probably fully custom. It's just for the most part binary compatible with 8080/z80, so no reshuffled opcodes, and devs could reuse a lot of code.
A lot of its behavior is different from the two 80's, such as `di ; halt`. We're also not sure the CPU has a 4-bit ALU. What's sure is that it's using a separate mechanism for 16-bit inc/dec, the same one it's using for PC increments, and it's at the source of the OAM inc/dec bug. I don't believe z80 or 8080 has that.
The French Lord of Laziness (and a huge Legend of Zelda fan)
https://github.com/ISSOtm
ASMu is laifu <3
Post Reply