rubygb 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/Gemfile.lock +13 -1
  4. data/README.md +3 -3
  5. data/lib/galp/memory.inc +153 -0
  6. data/lib/rgbds/LICENSE +32 -0
  7. data/lib/rubygb/cli.rb +26 -2
  8. data/lib/rubygb/template/template.rb +99 -0
  9. data/lib/rubygb/version.rb +1 -1
  10. data/lib/rubygb.rb +1 -0
  11. data/rubygb.gemspec +2 -0
  12. data/scrap/example/gbhw.inc +645 -0
  13. data/{lib/galp/EXAMPLE1.TXT → scrap/example/hello-world-with-comments.asm} +123 -113
  14. data/scrap/example/hello-world-with-comments.gb +0 -0
  15. data/scrap/example/hello-world-with-comments.map +65 -0
  16. data/scrap/example/hello-world-with-comments.obj +0 -0
  17. data/scrap/example/hello-world-with-comments.sym +12 -0
  18. data/scrap/example/ibmpc1.inc +2400 -0
  19. data/scrap/test/lib/gbhw.inc +645 -0
  20. data/scrap/test/lib/ibmpx1.inc +2400 -0
  21. data/scrap/test/lib/memory.inc +153 -0
  22. data/scrap/test/test.gb +0 -0
  23. data/scrap/test/test.map +65 -0
  24. data/scrap/test/test.obj +0 -0
  25. data/scrap/test/test.s +91 -0
  26. data/scrap/test/test.sym +12 -0
  27. metadata +37 -31
  28. data/lib/galp/AND.GIF +0 -0
  29. data/lib/galp/ARROW.GIF +0 -0
  30. data/lib/galp/C.BAT +0 -5
  31. data/lib/galp/CHANGES.TXT +0 -5
  32. data/lib/galp/EXAMPLE.LNK +0 -7
  33. data/lib/galp/GBSPEC.TXT +0 -1761
  34. data/lib/galp/INDEX.HTM +0 -23
  35. data/lib/galp/INSTR.HTM +0 -563
  36. data/lib/galp/MEM1.HTM +0 -199
  37. data/lib/galp/OPCODES.HTM +0 -351
  38. data/lib/galp/OR.GIF +0 -0
  39. data/lib/galp/README.TXT +0 -4
  40. data/lib/galp/REGS.HTM +0 -77
  41. data/lib/galp/RL.GIF +0 -0
  42. data/lib/galp/RLC.GIF +0 -0
  43. data/lib/galp/RR.GIF +0 -0
  44. data/lib/galp/RRC.GIF +0 -0
  45. data/lib/galp/SETUP.BAT +0 -7
  46. data/lib/galp/SLA.GIF +0 -0
  47. data/lib/galp/SRA.GIF +0 -0
  48. data/lib/galp/SRL.GIF +0 -0
  49. data/lib/galp/START.HTM +0 -24
  50. data/lib/galp/SWAP.GIF +0 -0
  51. data/lib/galp/VID1.HTM +0 -34
  52. data/lib/galp/XOR.GIF +0 -0
  53. /data/lib/galp/{GBHW.TXT → gbhw.inc} +0 -0
  54. /data/lib/galp/{IBMPC1.TXT → ibmpx1.inc} +0 -0
  55. /data/{lib/galp/MEMORY.TXT → scrap/example/memory.asm} +0 -0
data/lib/galp/GBSPEC.TXT DELETED
@@ -1,1761 +0,0 @@
1
- ============================================================================
2
- Everything You Always Wanted To Know About GAMEBOY *
3
- ============================================================================
4
-
5
- * but were afraid to ask
6
-
7
-
8
- Pan of -ATX- Document Updated by contributions from:
9
- Marat Fayzullin, Pascal Felber, Paul Robson, Martin Korth
10
-
11
- Last update 11-Mar-98 by kOOPa
12
-
13
- Forward: The following was typed up for informational purposes regarding
14
- the inner workings on the hand-held game machine known as
15
- GameBoy, manufactured and designed by Nintendo Co., LTD.
16
- This info is presented to inform a user on how their Game Boy
17
- works and what makes it "tick". GameBoy is copyrighted by
18
- Nintendo Co., LTD. Any reference to copyrighted material is
19
- not presented for monetary gain, but for educational purposes
20
- and higher learning.
21
-
22
- Terms
23
- -----
24
-
25
- GB = Original GameBoy
26
- GBP = GameBoy Pocket/GameBoy Light
27
- GBC = GameBoy Color
28
- SGB = Super GameBoy
29
-
30
-
31
- Game Boy Specs
32
- --------------
33
-
34
- CPU: 8-bit (Similar to the Z80 processor.)
35
- Main RAM: 8K Byte
36
- Video RAM: 8K Byte
37
- Screen Size 2.6"
38
- Resolution: 160x144 (20x18 tiles)
39
- Max # of sprites: 40
40
- Max # sprites/line: 10
41
- Max sprite size: 8x16
42
- Min sprite size: 8x8
43
- Clock Speed: 4.194304 MHz (4.295454 SGB, 4.194/8.388MHz GBC)
44
- Horiz Sync: 9198 KHz (9420 KHz for SGB)
45
- Vert Sync: 59.73 Hz (61.17 Hz for SGB)
46
- Sound: 4 channels with stereo sound
47
- Power: DC6V 0.7W (DC3V 0.7W for GB Pocket)
48
-
49
-
50
- Processor
51
- ---------
52
-
53
- The GameBoy uses a computer chip similar to an Intel 8080.
54
- It contains all of the instructions of an 8080 except there
55
- are no exchange instructions. In many ways the processor is
56
- more similar to the Zilog Z80 processor. Compared to the
57
- Z80, some instructions have been added and some have been
58
- taken away.
59
-
60
- The following are added instructions:
61
-
62
- ADD SP,nn ;nn = signed byte
63
- LDI (HL),A ;Write A to (HL) and increment HL
64
- LDD (HL),A ;Write A to (HL) and decrement HL
65
- LDI A,(HL) ;Write (HL) to A and increment HL
66
- LDD A,(HL) ;Write (HL) to A and decrement HL
67
- LD A,($FF00+nn)
68
- LD A,($FF00+C)
69
- LD ($FF00+nn),A
70
- LD ($FF00+C),A
71
- LD (nnnn),SP
72
- LD HL,(SP+nn) ;nn = signed byte
73
- STOP ;Stop processor & screen until button press
74
- SWAP r ;Swap high & low nibbles of r
75
-
76
- The following instructions have been removed:
77
-
78
- Any command that uses the IX or IY registers.
79
- All IN/OUT instructions.
80
- All exchange instructions.
81
- All commands prefixed by ED (except remapped RETI).
82
- All conditional jumps/calls/rets on parity/overflow and sign flag.
83
-
84
- The following instructions have different opcodes:
85
-
86
- LD A,[nnnn]
87
- LD [nnnn],A
88
- RETI
89
-
90
-
91
- General Memory Map* Hardware Write Registers
92
- ------------------ ------------------------
93
-
94
- Interrupt Enable Register
95
- --------------------------- FFFF
96
- Internal RAM
97
- --------------------------- FF80
98
- Empty but unusable for I/O
99
- --------------------------- FF4C
100
- I/O ports
101
- --------------------------- FF00
102
- Empty but unusable for I/O
103
- --------------------------- FEA0
104
- Sprite Attrib Memory (OAM)
105
- --------------------------- FE00
106
- Echo of 8kB Internal RAM
107
- --------------------------- E000
108
- 8kB Internal RAM
109
- --------------------------- C000 -------------------------
110
- 8kB switchable RAM bank / MBC1 ROM/RAM Select
111
- --------------------------- A000 / ------------------------
112
- 8kB Video RAM / / RAM Bank Select
113
- --------------------------- 8000 --/ / -----------------------
114
- 16kB switchable ROM bank 6000 ----/ / ROM Bank Select
115
- --------------------------- 4000 ------/ ----------------------
116
- 16kB ROM bank #0 2000 --------/ RAM Bank enable
117
- --------------------------- 0000 -------------------------------
118
-
119
- * NOTE: b = bit, B = byte
120
-
121
-
122
- Echo of 8kB Internal RAM
123
- ------------------------
124
-
125
- The addresses E000-FE00 appear to access the internal RAM
126
- the same as C000-DE00. (i.e. If you write a byte to address
127
- E000 it will appear at C000 and E000. Similarly, writing a
128
- byte to C000 will appear at C000 and E000.)
129
-
130
-
131
- User I/O
132
- --------
133
-
134
- There are no empty spaces in the memory map for
135
- implementing input ports except the switchable RAM bank
136
- area (not an option on the Super Smart Card since it's
137
- RAM bank is always enabled).
138
-
139
- An output only port may be implemented anywhere between
140
- A000-FDFF. If implemented in a RAM area care should be
141
- taken to use an area of RAM not used for anything else.
142
- (FE00 and above can't be used because the CPU doesn't
143
- generate an external /WR for these locations.)
144
-
145
- If you have a cart with an MBC1, a ROM 4Mbit or smaller,
146
- and a RAM 8Kbyte or smaller (or no RAM) then you can use
147
- pins 6 & 7 of the MBC1 for 2 digital output pins for
148
- whatever purpose you wish. To use them you must first
149
- put the MBC1 into 4MbitROM/32KbyteRAM mode by writing
150
- 01 to 6000. The two least significant bits you write
151
- to 4000 will then be output to these pins.
152
-
153
-
154
- Reserved Memory Locations
155
- -------------------------
156
-
157
- 0000 Restart $00 Address (RST $00 calls this address.)
158
-
159
- 0008 Restart $08 Address (RST $08 calls this address.)
160
-
161
- 0010 Restart $10 Address (RST $10 calls this address.)
162
-
163
- 0018 Restart $18 Address (RST $18 calls this address.)
164
-
165
- 0020 Restart $20 Address (RST $20 calls this address.)
166
-
167
- 0028 Restart $28 Address (RST $28 calls this address.)
168
-
169
- 0030 Restart $30 Address (RST $30 calls this address.)
170
-
171
- 0038 Restart $38 Address (RST $38 calls this address.)
172
-
173
- 0040 Vertical Blank Interrupt Start Address
174
-
175
- 0048 LCDC Status Interrupt Start Address
176
-
177
- 0050 Timer Overflow Interrupt Start Address
178
-
179
- 0058 Serial Transfer Completion Interrupt Start Address
180
-
181
- 0060 High-to-Low of P10-P13 Interrupt Start Address
182
-
183
- An internal information area is located at 0100-014F in
184
- each cartridge. It contains the following values:
185
-
186
- 0100-0103 This is the begin code execution point in a
187
- cart. Usually there is a NOP and a JP
188
- instruction here but not always.
189
-
190
- 0104-0133 Scrolling Nintendo graphic:
191
- CE ED 66 66 CC 0D 00 0B 03 73 00 83 00 0C 00 0D
192
- 00 08 11 1F 88 89 00 0E DC CC 6E E6 DD DD D9 99
193
- BB BB 67 63 6E 0E EC CC DD DC 99 9F BB B9 33 3E
194
- ( PROGRAM WON'T RUN IF CHANGED!!!)
195
-
196
- 0134-0142 Title of the game in UPPER CASE ASCII. If it
197
- is less than 16 characters then the remaining
198
- bytes are filled with 00's.
199
-
200
- 0143 $80 = Color GB, $00 or other = not Color GB
201
-
202
- 0144 Ascii hex digit, high nibble of licensee code (new).
203
- 0145 Ascii hex digit, low nibble of licensee code (new).
204
- (These are normally $00 if [$014B] <> $33.)
205
-
206
- 0146 GB/SGB Indicator (00 = GameBoy, 03 = Super GameBoy functions)
207
- (Super GameBoy functions won't work if <> $03.)
208
-
209
- 0147 Cartridge type:
210
- 0 - ROM ONLY 12 - ROM+MBC3+RAM
211
- 1 - ROM+MBC1 13 - ROM+MBC3+RAM+BATT
212
- 2 - ROM+MBC1+RAM 19 - ROM+MBC5
213
- 3 - ROM+MBC1+RAM+BATT 1A - ROM+MBC5+RAM
214
- 5 - ROM+MBC 1B - ROM+MBC5+RAM+BATT
215
- 6 - ROM+MBC2+BATTERY 1C - ROM+MBC5+RUMBLE
216
- 8 - ROM+RAM 1D - ROM+MBC5+RUMBLE+SRAM
217
- 9 - ROM+RAM+BATTERY 1E - ROM+MBC5+RUMBLE+SRAM+BATT
218
- B - ROM+MMM01 1F - Pocket Camera
219
- C - ROM+MMM01+SRAM FD - Bandai TAMA5
220
- D - ROM+MMM01+SRAM+BATT FE - Hudson HuC-3
221
- F - ROM+MBC3+TIMER+BATT FF - Hudson HuC-1
222
- 10 - ROM+MBC3+TIMER+RAM+BATT
223
- 11 - ROM+MBC3
224
-
225
- 0148 ROM size:
226
- 0 - 256Kbit = 32KByte = 2 banks
227
- 1 - 512Kbit = 64KByte = 4 banks
228
- 2 - 1Mbit = 128KByte = 8 banks
229
- 3 - 2Mbit = 256KByte = 16 banks
230
- 4 - 4Mbit = 512KByte = 32 banks
231
- 5 - 8Mbit = 1MByte = 64 banks
232
- 6 - 16Mbit = 2MByte = 128 banks
233
- $52 - 9Mbit = 1.1MByte = 72 banks
234
- $53 - 10Mbit = 1.2MByte = 80 banks
235
- $54 - 12Mbit = 1.5MByte = 96 banks
236
-
237
- 0149 RAM size:
238
- 0 - None
239
- 1 - 16kBit = 2kB = 1 bank
240
- 2 - 64kBit = 8kB = 1 bank
241
- 3 - 256kBit = 32kB = 4 banks
242
- 4 - 1MBit =128kB =16 banks
243
-
244
- 014A Destination code:
245
- 0 - Japanese
246
- 1 - Non-Japanese
247
-
248
- 014B Licensee code (old):
249
- 33 - Check 0144/0145 for Licensee code.
250
- 79 - Accolade
251
- A4 - Konami
252
- (Super GameBoy function won't work if <> $33.)
253
-
254
- 014C Mask ROM Version number (Usually $00)
255
-
256
- 014D Complement check
257
- (PROGRAM WON'T RUN ON GB IF NOT CORRECT!!!)
258
- (It will run on Super GB, however, if incorrect.)
259
-
260
- 014E-014F Checksum (higher byte first) produced by
261
- adding all bytes of a cartridge except for two
262
- checksum bytes and taking two lower bytes of
263
- the result. (GameBoy ignores this value.)
264
-
265
-
266
- Cartridge Types
267
- ---------------
268
-
269
- The following define the byte at cart location 0147:
270
-
271
- ROM ONLY
272
- This is a 32kB (256kb) ROM and occupies 0000-7FFF.
273
-
274
- MBC1 (Memory Bank Controller 1)
275
- MBC1 has two different maximum memory modes:
276
- 16Mbit ROM/8KByte RAM or 4Mbit ROM/32KByte RAM.
277
-
278
- The MBC1 defaults to 16Mbit ROM/8KByte RAM mode
279
- on power up. Writing a value (XXXXXXXS - X = Don't
280
- care, S = Memory model select) into 6000-7FFF area
281
- will select the memory model to use. S = 0 selects
282
- 16/8 mode. S = 1 selects 4/32 mode.
283
-
284
- Writing a value (XXXBBBBB - X = Don't cares, B =
285
- bank select bits) into 2000-3FFF area will select an
286
- appropriate ROM bank at 4000-7FFF. Values of 0 and 1
287
- do the same thing and point to ROM bank 1. Rom bank 0
288
- is not accessible from 4000-7FFF and can only be read
289
- from 0000-3FFF.
290
-
291
- If memory model is set to 4/32:
292
- Writing a value (XXXXXXBB - X = Don't care, B =
293
- bank select bits) into 4000-5FFF area will select an
294
- appropriate RAM bank at A000-C000. Before you can
295
- read or write to a RAM bank you have to enable it by
296
- writing a XXXX1010 into 0000-1FFF area*. To disable
297
- RAM bank operations write any value but XXXX1010
298
- into 0000-1FFF area. Disabling a RAM bank probably
299
- protects that bank from false writes during power
300
- down of the GameBoy. (NOTE: Nintendo suggests values
301
- $0A to enable and $00 to disable RAM bank!!)
302
-
303
- If memory model is set to 16/8 mode:
304
- Writing a value (XXXXXXBB - X = Don't care, B =
305
- bank select bits) into 4000-5FFF area will set the
306
- two most significant ROM address lines.
307
-
308
- * NOTE: The Super Smart Card doesn't require this
309
- operation because it's RAM bank is ALWAYS enabled.
310
- Include this operation anyway to allow your code
311
- to work with both.
312
-
313
- MBC2 (Memory Bank Controller 2):
314
- This memory controller works much like the MBC1
315
- controller with the following exceptions:
316
-
317
- MBC2 will work with ROM sizes up to 2Mbit.
318
-
319
- Writing a value (XXXXBBBB - X = Don't cares, B =
320
- bank select bits) into 2000-3FFF area will select an
321
- appropriate ROM bank at 4000-7FFF.
322
-
323
- RAM switching is not provided. Unlike the MBC1 which
324
- uses external RAM, MBC2 has 512 x 4 bits of RAM which
325
- is in the controller itself. It still requires an
326
- external battery to save data during power-off though.
327
-
328
- The least significant bit of the upper address byte
329
- must be zero to enable/disable cart RAM. For example
330
- the following addresses can be used to enable/disable
331
- cart RAM:
332
- 0000-00FF, 0200-02FF, 0400-04FF, ..., 1E00-1EFF.
333
- The suggested address range to use for MBC2 ram
334
- enable/disable is 0000-00FF.
335
-
336
- The least significant bit of the upper address byte
337
- must be one to select a ROM bank. For example the
338
- following addresses can be used to select a ROM bank:
339
- 2100-21FF, 2300-23FF, 2500-25FF, ..., 3F00-3FFF.
340
- The suggested address range to use for MBC2 rom
341
- bank selection is 2100-21FF.
342
-
343
- MBC3 (Memory Bank Controller 3):
344
- This controller is similar to MBC1 except it accesses
345
- all 16mbits of ROM without requiring any writes to the
346
- 4000-5FFF area.
347
- Writing a value (XBBBBBBB - X = Don't care, B =
348
- bank select bits) into 2000-3FFF area will select an
349
- appropriate ROM bank at 4000-7FFF.
350
-
351
- Also, this MBC has a built-in battery-backed Real
352
- Time Clock (RTC) not found in any other MBC. Some
353
- MBC3 carts do not support it (WarioLand II non-color
354
- version) but some do (Harvest Moon/Japanese version.)
355
-
356
- MBC5 (Memory Bank Controller 5):
357
- This controller is the first MBC that is guaranteed
358
- to run in GameBoy Color double-speed mode but it
359
- appears the other MBC's run fine in GBC double-speed
360
- mode as well.
361
-
362
- It is similar to the MBC3 (but no RTC) but can
363
- access up to 64mbits of ROM and up to 1mbit of RAM.
364
- The lower 8 bits of the 9-bit rom bank select is
365
- written to the 2000-2FFF area while the upper bit
366
- is written to the least significant bit of the
367
- 3000-3FFF area.
368
-
369
- Also, this is the first MBC that allows rom bank 0
370
- to appear in the 4000-7FFF range by writing $000
371
- to the rom bank select.
372
-
373
- HuC1 (Memory Bank / Infrared Controller):
374
- This controller made by Hudson Soft appears to be
375
- very similar to an MBC1 with the main difference
376
- being that it supports infrared LED input / output.
377
- The Japanese cart "Fighting Phoenix" (internal cart
378
- name: SUPER B DAMAN) is known to contain this chip.
379
-
380
-
381
- Power Up Sequence
382
- -----------------
383
-
384
- When the GameBoy is powered up, a 256 byte program
385
- starting at memory location 0 is executed. This program
386
- is located in a ROM inside the GameBoy. The first thing
387
- the program does is read the cartridge locations from
388
- $104 to $133 and place this graphic of a Nintendo logo
389
- on the screen at the top. This image is then scrolled
390
- until it is in the middle of the screen. Two musical
391
- notes are then played on the internal speaker. Again,
392
- the cartridge locations $104 to $133 are read but this
393
- time they are compared with a table in the internal rom.
394
- If any byte fails to compare, then the GameBoy stops
395
- comparing bytes and simply halts all operations.
396
-
397
- GB & GB Pocket:
398
- Next, the GameBoy starts adding all of the bytes
399
- in the cartridge from $134 to $14d. A value of 25
400
- decimal is added to this total. If the least
401
- significant byte of the result is a not a zero,
402
- then the GameBoy will stop doing anything.
403
-
404
- Super GB:
405
- Even though the GB & GBP check the memory locations
406
- from $134 to $14d, the SGB doesn't.
407
-
408
- If the above checks pass then the internal ROM is
409
- disabled and cartridge program execution begins at
410
- location $100 with the following register values:
411
-
412
- AF=$01-GB/SGB, $FF-GBP, $11-GBC
413
- F =$B0
414
- BC=$0013
415
- DE=$00D8
416
- HL=$014D
417
- Stack Pointer=$FFFE
418
- [$FF05] = $00 ; TIMA
419
- [$FF06] = $00 ; TMA
420
- [$FF07] = $00 ; TAC
421
- [$FF10] = $80 ; NR10
422
- [$FF11] = $BF ; NR11
423
- [$FF12] = $F3 ; NR12
424
- [$FF14] = $BF ; NR14
425
- [$FF16] = $3F ; NR21
426
- [$FF17] = $00 ; NR22
427
- [$FF19] = $BF ; NR24
428
- [$FF1A] = $7F ; NR30
429
- [$FF1B] = $FF ; NR31
430
- [$FF1C] = $9F ; NR32
431
- [$FF1E] = $BF ; NR33
432
- [$FF20] = $FF ; NR41
433
- [$FF21] = $00 ; NR42
434
- [$FF22] = $00 ; NR43
435
- [$FF23] = $BF ; NR30
436
- [$FF24] = $77 ; NR50
437
- [$FF25] = $F3 ; NR51
438
- [$FF26] = $F1-GB, $F0-SGB ; NR52
439
- [$FF40] = $91 ; LCDC
440
- [$FF42] = $00 ; SCY
441
- [$FF43] = $00 ; SCX
442
- [$FF45] = $00 ; LYC
443
- [$FF47] = $FC ; BGP
444
- [$FF48] = $FF ; OBP0
445
- [$FF49] = $FF ; OBP1
446
- [$FF4A] = $00 ; WY
447
- [$FF4B] = $00 ; WX
448
- [$FFFF] = $00 ; IE
449
-
450
- It is not a good idea to assume the above values
451
- will always exist. A later version GameBoy could
452
- contain different values than these at reset.
453
- Always set these registers on reset rather than
454
- assume they are as above.
455
-
456
- Please note that GameBoy internal RAM on power up
457
- contains random data. All of the GameBoy emulators
458
- tend to set all RAM to value $00 on entry.
459
-
460
- Cart RAM the first time it is accessed on a real
461
- GameBoy contains random data. It will only contain
462
- known data if the GameBoy code initializes it to
463
- some value.
464
-
465
- Stop Mode
466
- ---------
467
-
468
- The STOP command halts the GameBoy processor
469
- and screen until any button is pressed. The GB
470
- and GBP screen goes white with a single dark
471
- horizontal line. The GBC screen goes black.
472
-
473
-
474
- Low-Power Mode
475
- --------------
476
-
477
- It is recommended that the HALT instruction be used
478
- whenever possible to reduce power consumption & extend
479
- the life of the batteries. This command stops the
480
- system clock reducing the power consumption of both
481
- the CPU and ROM.
482
-
483
- The CPU will remain suspended until an interrupt
484
- occurs at which point the interrupt is serviced and
485
- then the instruction immediately following the HALT
486
- is executed. If interrupts are disabled (DI) then
487
- halt doesn't suspend operation but it does cause
488
- the program counter to stop counting for one
489
- instruction on the GB,GBP, and SGB as mentioned below.
490
-
491
- Depending on how much CPU time is required by a game,
492
- the HALT instruction can extend battery life anywhere
493
- from 5 to 50% or possibly more.
494
-
495
- WARNING: The instruction immediately following the
496
- HALT instruction is "skipped" when interrupts are
497
- disabled (DI) on the GB,GBP, and SGB. As a result,
498
- always put a NOP after the HALT instruction. This
499
- instruction skipping doesn't occur when interrupts
500
- are enabled (EI).
501
- This "skipping" does not seem to occur on the
502
- GameBoy Color even in regular GB mode. ($143=$00)
503
-
504
- EXAMPLES from Martin Korth who documented this problem:
505
- (assuming interrupts disabled for all examples)
506
-
507
- 1) This code causes the 'a' register to be incremented TWICE.
508
- 76 halt
509
- 3C inc a
510
-
511
- 2) The next example is a bit more difficult. The following code
512
- 76 halt
513
- FA 34 12 ld a,(1234)
514
-
515
- is effectively executed as
516
-
517
- 76 halt
518
- FA FA 34 ld a,(34FA)
519
- 12 ld (de),a
520
-
521
- 3) Finally an interesting side effect
522
- 76 halt
523
- 76 halt
524
-
525
- This combination hangs the cpu.
526
- The first HALT causes the second HALT to be repeated, which
527
- therefore causes the following command (=itself) to be
528
- repeated - again and again.
529
- Placing a NOP between the two halts would cause the NOP to
530
- be repeated once, the second HALT wouldn't lock the cpu.
531
-
532
- Below is suggested code for GameBoy programs:
533
-
534
- ; **** Main Game Loop ****
535
- Main:
536
- halt ; stop system clock
537
- ; return from halt when interrupted
538
- nop ; (See WARNING above.)
539
-
540
- ld a,(VblnkFlag)
541
- or a ; V-Blank interrupt ?
542
- jr z,Main ; No, some other interrupt
543
-
544
- xor a
545
- ld (VblnkFlag),a ; Clear V-Blank flag
546
-
547
- call Controls ; button inputs
548
- call Game ; game operation
549
-
550
- jr Main
551
-
552
- ; **** V-Blank Interrupt Routine ****
553
- Vblnk:
554
- push af
555
- push bc
556
- push de
557
- push hl
558
-
559
- call SpriteDma ; Do sprite updates
560
-
561
- ld a,1
562
- ld (VblnkFlag),a
563
-
564
- pop hl
565
- pop de
566
- pop bc
567
- pop af
568
- reti
569
-
570
-
571
- Video
572
- -----
573
-
574
- The main GameBoy screen buffer (background) consists
575
- of 256x256 pixels or 32x32 tiles (8x8 pixels each). Only
576
- 160x144 pixels can be displayed on the screen. Registers
577
- SCROLLX and SCROLLY hold the coordinates of background to
578
- be displayed in the left upper corner of the screen.
579
- Background wraps around the screen (i.e. when part of it
580
- goes off the screen, it appears on the opposite side.)
581
-
582
- An area of VRAM known as Background Tile Map contains
583
- the numbers of tiles to be displayed. It is organized as
584
- 32 rows of 32 bytes each. Each byte contains a number of
585
- a tile to be displayed. Tile patterns are taken from the
586
- Tile Data Table located either at $8000-8FFF or
587
- $8800-97FF. In the first case, patterns are numbered with
588
- unsigned numbers from 0 to 255 (i.e. pattern #0 lies at
589
- address $8000). In the second case, patterns have signed
590
- numbers from -128 to 127 (i.e. pattern #0 lies at address
591
- $9000). The Tile Data Table address for the background
592
- can be selected via LCDC register.
593
-
594
- Besides background, there is also a "window" overlaying
595
- the background. The window is not scrollable i.e. it is
596
- always displayed starting from its left upper corner. The
597
- location of a window on the screen can be adjusted via
598
- WNDPOSX and WNDPOSY registers. Screen coordinates of the
599
- top left corner of a window are WNDPOSX-7,WNDPOSY. The
600
- tile numbers for the window are stored in the Tile Data
601
- Table. None of the windows tiles are ever transparent.
602
- Both the Background and the window share the same Tile
603
- Data Table.
604
-
605
- Both background and window can be disabled or enabled
606
- separately via bits in the LCDC register.
607
-
608
- If the window is used and a scan line interrupt disables
609
- it (either by writing to LCDC or by setting WX > 166)
610
- and a scan line interrupt a little later on enables it
611
- then the window will resume appearing on the screen at the
612
- exact position of the window where it left off earlier.
613
- This way, even if there are only 16 lines of useful graphics
614
- in the window, you could display the first 8 lines at the
615
- top of the screen and the next 8 lines at the bottom if
616
- you wanted to do so.
617
-
618
- WX may be changed during a scan line interrupt (to either
619
- cause a graphic distortion effect or to disable the window
620
- (WX>166) ) but changes to WY are not dynamic and won't
621
- be noticed until the next screen redraw.
622
-
623
- The tile images are stored in the Tile Pattern Tables.
624
- Each 8x8 image occupies 16 bytes, where each 2 bytes
625
- represent a line:
626
-
627
- Tile: Image:
628
-
629
- .33333.. .33333.. -> 01111100 -> $7C
630
- 22...22. 01111100 -> $7C
631
- 11...11. 22...22. -> 00000000 -> $00
632
- 2222222. <-- digits 11000110 -> $C6
633
- 33...33. represent 11...11. -> 11000110 -> $C6
634
- 22...22. color 00000000 -> $00
635
- 11...11. numbers 2222222. -> 00000000 -> $00
636
- ........ 11111110 -> $FE
637
- 33...33. -> 11000110 -> $C6
638
- 11000110 -> $C6
639
- 22...22. -> 00000000 -> $00
640
- 11000110 -> $C6
641
- 11...11. -> 11000110 -> $C6
642
- 00000000 -> $00
643
- ........ -> 00000000 -> $00
644
- 00000000 -> $00
645
-
646
- As it was said before, there are two Tile Pattern Tables
647
- at $8000-8FFF and at $8800-97FF. The first one can be used
648
- for sprites, the background, and the window display. Its
649
- tiles are numbered from 0 to 255. The second table can be
650
- used for the background and the window display and its tiles
651
- are numbered from -128 to 127.
652
-
653
-
654
- Sprites
655
- ------
656
-
657
- GameBoy video controller can display up to 40 sprites
658
- either in 8x8 or in 8x16 pixels. Because of a limitation
659
- of hardware, only ten sprites can be displayed per scan
660
- line. Sprite patterns have the same format as tiles, but
661
- they are taken from the Sprite Pattern Table located at
662
- $8000-8FFF and have unsigned numbering. Sprite
663
- attributes reside in the Sprite Attribute Table (OAM
664
- - Object Attribute Memory) at $FE00-FE9F. OAM is divided
665
- into 40 4-byte blocks each of which corresponds to a sprite.
666
-
667
- In 8x16 sprite mode, the least significant bit of the
668
- sprite pattern number is ignored and treated as 0.
669
-
670
- When sprites with different x coordinate values overlap,
671
- the one with the smaller x coordinate (closer to the left)
672
- will have priority and appear above any others.
673
-
674
- When sprites with the same x coordinate values overlap,
675
- they have priority according to table ordering. (i.e.
676
- $FE00 - highest, $FE04 - next highest, etc.)
677
-
678
- Please note that Sprite X=0, Y=0 hides a sprite. To
679
- display a sprite use the following formulas:
680
-
681
- SpriteScreenPositionX(Upper left corner of sprite) = SpriteX - 8
682
- SpriteScreenPositionY(Upper left corner of sprite) = SpriteY - 16
683
-
684
- To display a sprite in the upper left corner of the
685
- screen set sprite X=8, Y=16.
686
-
687
- Only 10 sprites can be displayed on any one line.
688
- When this limit is exceeded, the lower priority sprites
689
- (priorities listed above) won't be displayed. To keep
690
- unused sprites from affecting onscreen sprites set their
691
- Y coordinate to Y=0 or Y=>144+16. Just setting the X
692
- coordinate to X=0 or X=>160+8 on a sprite will hide it
693
- but it will still affect other sprites sharing the same
694
- lines.
695
-
696
- Blocks have the following
697
- format:
698
-
699
- Byte0 Y position on the screen
700
- Byte1 X position on the screen
701
- Byte2 Pattern number 0-255 (Unlike some tile
702
- numbers, sprite pattern numbers are unsigned.
703
- LSB is ignored (treated as 0) in 8x16 mode.)
704
- Byte3 Flags:
705
-
706
- Bit7 Priority
707
- If this bit is set to 0, sprite is displayed
708
- on top of background & window. If this bit
709
- is set to 1, then sprite will be hidden behind
710
- colors 1, 2, and 3 of the background & window.
711
- (Sprite only prevails over color 0 of BG & win.)
712
- Bit6 Y flip
713
- Sprite pattern is flipped vertically if
714
- this bit is set to 1.
715
- Bit5 X flip
716
- Sprite pattern is flipped horizontally if
717
- this bit is set to 1.
718
- Bit4 Palette number
719
- Sprite colors are taken from OBJ1PAL if
720
- this bit is set to 1 and from OBJ0PAL
721
- otherwise.
722
-
723
-
724
- Sprite RAM Bug
725
- --------------
726
-
727
- There is a flaw in the GameBoy hardware that causes
728
- trash to be written to OAM RAM if the following commands
729
- are used while their 16-bit content is in the range
730
- of $FE00 to $FEFF:
731
-
732
- inc xx (xx = bc,de, or hl)
733
- dec xx
734
-
735
- ldi a,(hl)
736
- ldd a,(hl)
737
-
738
- ldi (hl),a
739
- ldd (hl),a
740
-
741
- Only sprites 1 & 2 ($FE00 & $FE04) are not affected
742
- by these instructions.
743
-
744
-
745
- Sound
746
- -----
747
-
748
- There are two sound channels connected to the output
749
- terminals SO1 and SO2. There is also a input terminal Vin
750
- connected to the cartridge. It can be routed to either of
751
- both output terminals. GameBoy circuitry allows producing
752
- sound in four different ways:
753
-
754
- Quadrangular wave patterns with sweep and envelope functions.
755
- Quadrangular wave patterns with envelope functions.
756
- Voluntary wave patterns from wave RAM.
757
- White noise with an envelope function.
758
-
759
- These four sounds can be controlled independantly and
760
- then mixed separately for each of the output terminals.
761
-
762
- Sound registers may be set at all times while producing
763
- sound.
764
-
765
- When setting the initial value of the envelope and
766
- restarting the length counter, set the initial flag to 1
767
- and initialize the data.
768
-
769
- Under the following situations the Sound ON flag is
770
- reset and the sound output stops:
771
-
772
- 1. When the sound output is stopped by the length counter.
773
- 2. When overflow occurs at the addition mode while sweep
774
- is operating at sound 1.
775
-
776
- When the Sound OFF flag for sound 3 (bit 7 of NR30) is
777
- set at 0, the cancellation of the OFF mode must be done
778
- by setting the sound OFF flag to 1. By initializing
779
- sound 3, it starts it's function.
780
-
781
- When the All Sound OFF flag (bit 7 of NR52) is set to 0,
782
- the mode registers for sounds 1,2,3, and 4 are reset and
783
- the sound output stops. (NOTE: The setting of each sounds
784
- mode register must be done after the All Sound OFF mode
785
- is cancelled. During the All Sound OFF mode, each sound
786
- mode register cannot be set.)
787
-
788
- NOTE: DURING THE ALL SOUND OFF MODE, GB POWER CONSUMPTION
789
- DROPS BY 16% OR MORE! WHILE YOUR PROGRAMS AREN'T USING
790
- SOUND THEN SET THE ALL SOUND OFF FLAG TO 0. IT DEFAULTS
791
- TO 1 ON RESET.
792
-
793
- These tend to be the two most important equations in
794
- converting between Hertz and GB frequency registers:
795
- (Sounds will have a 2.4% higher frequency on Super GB.)
796
-
797
- gb = 2048 - (131072 / Hz)
798
-
799
- Hz = 131072 / (2048 - gb)
800
-
801
-
802
- Timer
803
- -----
804
-
805
- Sometimes it's useful to have a timer that interrupts at
806
- regular intervals for routines that require periodic or
807
- percise updates. The timer in the GameBoy has a selectable
808
- frequency of 4096, 16384, 65536, or 262144 Hertz. This
809
- frequency increments the Timer Counter (TIMA). When it
810
- overflows, it generates an interrupt. It is then loaded
811
- with the contents of Timer Modulo (TMA). The following
812
- are examples:
813
-
814
- ;This interval timer interrupts 4096 times per second
815
-
816
- ld a,-1
817
- ld ($FF06),a ;Set TMA to divide clock by 1
818
- ld a,4
819
- ld ($FF07),a ;Set clock to 4096 Hertz
820
-
821
- ;This interval timer interrupts 65536 times per second
822
-
823
- ld a,-4
824
- ld ($FF06),a ;Set TMA to divide clock by 4
825
- ld a,5
826
- ld ($FF07),a ;Set clock to 262144 Hertz
827
-
828
-
829
- Serial I/O
830
- ----------
831
-
832
- The serial I/O port on the Gameboy is a very simple setup
833
- and is crude compared to standard RS-232 (IBM-PC) or RS-485
834
- (Macintosh) serial ports. There are no start or stop bits
835
- so the programmer must be more creative when using this port.
836
-
837
- During a transfer, a byte is shifted in at the same time
838
- that a byte is shifted out. The rate of the shift is deter-
839
- mined by whether the clock source is internal or external.
840
- If internal, the bits are shifted out at a rate of 8192Hz
841
- (122 microseconds) per bit. The most significant bit is
842
- shifted in and out first.
843
-
844
- When the internal clock is selected, it drives the clock
845
- pin on the game link port and it stays high when not used.
846
- During a transfer it will go low eight times to clock
847
- in/out each bit.
848
-
849
- A programmer initates a serial transfer by setting bit 7
850
- of $FF02. This bit may be read and is automatically set
851
- to 0 at the completion of transfer. After this bit is set,
852
- an interrupt will then occur eight bit clocks later if the
853
- serial interrupt is enabled.
854
- If internal clock is selected and serial interrupt is
855
- enabled, this interrupt occurs 122*8 microseconds later.
856
- If external clock is selected and serial interrupt is
857
- enabled, an interrupt will occur eight bit clocks later.
858
-
859
- Initiating a serial transfer with external clock will
860
- wait forever if no external clock is present. This allows
861
- a certain amount of synchronization with each serial port.
862
-
863
- The state of the last bit shifted out determines the
864
- state of the output line until another transfer takes
865
- place.
866
-
867
- If a serial transfer with internal clock is performed
868
- and no external GameBoy is present, a value of $FF will
869
- be received in the transfer.
870
-
871
- The following code causes $75 to be shifted out the
872
- serial port and a byte to be shifted into $FF01:
873
-
874
- ld a,$75
875
- ld ($FF01),a
876
- ld a,$81
877
- ld ($FF02),a
878
-
879
-
880
- Interrupt Procedure
881
- -------------------
882
-
883
- The IME (interrupt master enable) flag is reset by DI and
884
- prohibits all interrupts. It is set by EI and acknowledges
885
- the interrupt setting by the IE register.
886
-
887
- 1. When an interrupt is generated, the IF flag will be set.
888
- 2. If the IME flag is set & the corresponding IE flag is
889
- set, the following 3 steps are performed.
890
- 3. Reset the IME flag and prevent all interrupts.
891
- 4. The PC (program counter) is pushed onto the stack.
892
- 5. Jump to the starting address of the interrupt.
893
-
894
- Resetting of the IF register, which was the cause of the
895
- interrupt, is done by hardware.
896
-
897
- During the interrupt, pushing of registers to be used
898
- should be performed by the interrupt routine.
899
-
900
- Once the interrupt service is in progress, all the
901
- interrupts will be prohibited. However, if the IME flag
902
- and the IE flag are controlled, a number of interrupt
903
- services can be made possible by nesting.
904
-
905
- Return from an interrupt routine can be performed by
906
- either RETI or RET instruction.
907
-
908
- The RETI instruction enables interrupts after doing a
909
- return operation.
910
-
911
- If a RET is used as the final instruction in an interrupt
912
- routine, interrupts will remain disabled unless a EI was
913
- used in the interrupt routine or is used at a later time.
914
-
915
- The interrupt will be acknowledged during opcode fetch
916
- period of each instruction.
917
-
918
-
919
- Interrupt Descriptions
920
- ----------------------
921
-
922
- The following interrupts only occur if they have been
923
- enabled in the Interrupt Enable register ($FFFF) and
924
- if the interrupts have actually been enabled using the
925
- EI instruction.
926
-
927
- V-Blank -
928
-
929
- The V-Blank interrupt occurs ~59.7 times a second
930
- on a regular GB and ~61.1 times a second on a Super
931
- GB (SGB). This interrupt occurs at the beginning of
932
- the V-Blank period. During this period video hardware
933
- is not using video ram so it may be freely accessed.
934
- This period lasts approximately 1.1 milliseconds.
935
-
936
- LCDC Status -
937
-
938
- There are various reasons for this interrupt to occur
939
- as described by the STAT register ($FF40). One very
940
- popular reason is to indicate to the user when the
941
- video hardware is about to redraw a given LCD line.
942
- This can be useful for dynamically controlling the SCX/
943
- SCY registers ($FF43/$FF42) to perform special video
944
- effects.
945
-
946
- Timer Overflow -
947
-
948
- This interrupt occurs when the TIMA register ($FF05)
949
- changes from $FF to $00.
950
-
951
- Serial Transfer Completion -
952
-
953
- This interrupt occurs when a serial transfer has
954
- completed on the game link port.
955
-
956
- High-to-Low of P10-P13 -
957
-
958
- This interrupt occurs on a transition of any of the
959
- keypad input lines from high to low. Due to the fact
960
- that keypad "bounce"* is virtually always present,
961
- software should expect this interrupt to occur one
962
- or more times for every button press and one or more
963
- times for every button release.
964
-
965
- * - Bounce tends to be a side effect of any button
966
- making or breaking a connection. During these
967
- periods, it is very common for a small amount of
968
- oscillation between high & low states to take place.
969
-
970
- I/O Registers
971
- -------------
972
-
973
- FF00
974
- Name - P1
975
- Contents - Register for reading joy pad info
976
- and determining system type. (R/W)
977
-
978
- Bit 7 - Not used
979
- Bit 6 - Not used
980
- Bit 5 - P15 out port
981
- Bit 4 - P14 out port
982
- Bit 3 - P13 in port
983
- Bit 2 - P12 in port
984
- Bit 1 - P11 in port
985
- Bit 0 - P10 in port
986
-
987
- This is the matrix layout for register $FF00:
988
-
989
-
990
- P14 P15
991
- | |
992
- P10-------O-Right----O-A
993
- | |
994
- P11-------O-Left-----O-B
995
- | |
996
- P12-------O-Up-------O-Select
997
- | |
998
- P13-------O-Down-----O-Start
999
- | |
1000
-
1001
- Example code:
1002
-
1003
- Game: Ms. Pacman
1004
- Address: $3b1
1005
-
1006
- LD A,$20 <- bit 5 = $20
1007
- LD ($FF00),A <- select P14 by setting it low
1008
- LD A,($FF00)
1009
- LD A,($FF00) <- wait a few cycles
1010
- CPL <- complement A
1011
- AND $0F <- get only first 4 bits
1012
- SWAP A <- swap it
1013
- LD B,A <- store A in B
1014
- LD A,$10
1015
- LD ($FF00),A <- select P15 by setting it low
1016
- LD A,($FF00)
1017
- LD A,($FF00)
1018
- LD A,($FF00)
1019
- LD A,($FF00)
1020
- LD A,($FF00)
1021
- LD A,($FF00) <- Wait a few MORE cycles
1022
- CPL <- complement (invert)
1023
- AND $0F <- get first 4 bits
1024
- OR B <- put A and B together
1025
-
1026
- LD B,A <- store A in D
1027
- LD A,($FF8B) <- read old joy data from ram
1028
- XOR B <- toggle w/current button bit
1029
- AND B <- get current button bit back
1030
- LD ($FF8C),A <- save in new Joydata storage
1031
- LD A,B <- put original value in A
1032
- LD ($FF8B),A <- store it as old joy data
1033
-
1034
-
1035
- LD A,$30 <- deselect P14 and P15
1036
- LD ($FF00),A <- RESET Joypad
1037
- RET <- Return from Subroutine
1038
-
1039
- The button values using the above method are such:
1040
- $80 - Start $8 - Down
1041
- $40 - Select $4 - Up
1042
- $20 - B $2 - Left
1043
- $10 - A $1 - Right
1044
-
1045
- Let's say we held down A, Start, and Up.
1046
- The value returned in accumulator A would be $94
1047
-
1048
-
1049
- FF01
1050
- Name - SB
1051
- Contents - Serial transfer data (R/W)
1052
-
1053
- 8 Bits of data to be read/written
1054
-
1055
- FF02
1056
- Name - SC
1057
- Contents - SIO control (R/W)
1058
-
1059
- Bit 7 - Transfer Start Flag
1060
- 0: Non transfer
1061
- 1: Start transfer
1062
-
1063
- Bit 0 - Shift Clock
1064
- 0: External Clock (500KHz Max.)
1065
- 1: Internal Clock (8192Hz)
1066
-
1067
- Transfer is initiated by setting the
1068
- Transfer Start Flag. This bit may be read
1069
- and is automatically set to 0 at the end of
1070
- Transfer.
1071
-
1072
- Transmitting and receiving serial data is
1073
- done simultaneously. The received data is
1074
- automatically stored in SB.
1075
-
1076
- FF04
1077
- Name - DIV
1078
- Contents - Divider Register (R/W)
1079
-
1080
- This register is incremented 16384 (~16779
1081
- on SGB) times a second. Writing any value
1082
- sets it to $00.
1083
- FF05
1084
- Name - TIMA
1085
- Contents - Timer counter (R/W)
1086
-
1087
- This timer is incremented by a clock frequency
1088
- specified by the TAC register ($FF07). The timer
1089
- generates an interrupt when it overflows.
1090
-
1091
- FF06
1092
- Name - TMA
1093
- Contents - Timer Modulo (R/W)
1094
-
1095
- When the TIMA overflows, this data will be loaded.
1096
-
1097
- FF07
1098
- Name - TAC
1099
- Contents - Timer Control (R/W)
1100
-
1101
- Bit 2 - Timer Stop
1102
- 0: Stop Timer
1103
- 1: Start Timer
1104
-
1105
- Bits 1+0 - Input Clock Select
1106
- 00: 4.096 KHz (~4.194 KHz SGB)
1107
- 01: 262.144 KHz (~268.4 KHz SGB)
1108
- 10: 65.536 KHz (~67.11 KHz SGB)
1109
- 11: 16.384 KHz (~16.78 KHz SGB)
1110
-
1111
- FF0F
1112
- Name - IF
1113
- Contents - Interrupt Flag (R/W)
1114
-
1115
- Bit 4: Transition from High to Low of Pin number P10-P13
1116
- Bit 3: Serial I/O transfer complete
1117
- Bit 2: Timer Overflow
1118
- Bit 1: LCDC (see STAT)
1119
- Bit 0: V-Blank
1120
-
1121
- The priority and jump address for the above 5 interrupts are:
1122
-
1123
- Interrupt Priority Start Address
1124
-
1125
- V-Blank 1 $0040
1126
- LCDC Status 2 $0048 - Modes 0, 1, 2
1127
- LYC=LY coincide (selectable)
1128
- Timer Overflow 3 $0050
1129
- Serial Transfer 4 $0058 - when transfer is complete
1130
- Hi-Lo of P10-P13 5 $0060
1131
-
1132
- * When more than 1 interrupts occur at the same time
1133
- only the interrupt with the highest priority can be
1134
- acknowledged. When an interrupt is used a '0' should
1135
- be stored in the IF register before the IE register
1136
- is set.
1137
-
1138
-
1139
- FF10
1140
- Name - NR 10
1141
- Contents - Sound Mode 1 register, Sweep register (R/W)
1142
-
1143
- Bit 6-4 - Sweep Time
1144
- Bit 3 - Sweep Increase/Decrease
1145
- 0: Addition (frequency increases)
1146
- 1: Subtraction (frequency decreases)
1147
- Bit 2-0 - Number of sweep shift (n: 0-7)
1148
-
1149
- Sweep Time: 000: sweep off - no freq change
1150
- 001: 7.8 ms (1/128Hz)
1151
- 010: 15.6 ms (2/128Hz)
1152
- 011: 23.4 ms (3/128Hz)
1153
- 100: 31.3 ms (4/128Hz)
1154
- 101: 39.1 ms (5/128Hz)
1155
- 110: 46.9 ms (6/128Hz)
1156
- 111: 54.7 ms (7/128Hz)
1157
-
1158
- The change of frequency (NR13,NR14) at each shift
1159
- is calculated by the following formula where
1160
- X(0) is initial freq & X(t-1) is last freq:
1161
-
1162
- X(t) = X(t-1) +/- X(t-1)/2^n
1163
-
1164
- FF11
1165
- Name - NR 11
1166
- Contents - Sound Mode 1 register, Sound length/Wave pattern duty (R/W)
1167
-
1168
- Only Bits 7-6 can be read.
1169
-
1170
- Bit 7-6 - Wave Pattern Duty
1171
- Bit 5-0 - Sound length data (t1: 0-63)
1172
-
1173
- Wave Duty: 00: 12.5% ( _--------_--------_-------- )
1174
- 01: 25% ( __-------__-------__------- )
1175
- 10: 50% ( ____-----____-----____----- ) (default)
1176
- 11: 75% ( ______---______---______--- )
1177
-
1178
- Sound Length = (64-t1)*(1/256) seconds
1179
- FF12
1180
- Name - NR 12
1181
- Contents - Sound Mode 1 register, Envelope (R/W)
1182
-
1183
- Bit 7-4 - Initial volume of envelope
1184
- Bit 3 - Envelope UP/DOWN
1185
- 0: Attenuate
1186
- 1: Amplify
1187
- Bit 2-0 - Number of envelope sweep (n: 0-7)
1188
- (If zero, stop envelope operation.)
1189
-
1190
- Initial volume of envelope is from 0 to $F.
1191
- Zero being no sound.
1192
-
1193
- Length of 1 step = n*(1/64) seconds
1194
-
1195
- FF13
1196
- Name - NR 13
1197
- Contents - Sound Mode 1 register, Frequency lo (W)
1198
-
1199
- Lower 8 bits of 11 bit frequency (x).
1200
- Next 3 bit are in NR 14 ($FF14)
1201
-
1202
- FF14
1203
- Name - NR 14
1204
- Contents - Sound Mode 1 register, Frequency hi (R/W)
1205
-
1206
- Only Bit 6 can be read.
1207
-
1208
- Bit 7 - Initial (when set, sound restarts)
1209
- Bit 6 - Counter/consecutive selection
1210
- Bit 2-0 - Frequency's higher 3 bits (x)
1211
-
1212
- Frequency = 4194304/(32*(2048-x)) Hz
1213
- = 131072/(2048-x) Hz
1214
-
1215
- Counter/consecutive Selection
1216
- 0 = Regardless of the length data in NR11
1217
- sound can be produced consecutively.
1218
- 1 = Sound is generated during the time period
1219
- set by the length data in NR11. After this
1220
- period the sound 1 ON flag (bit 0 of NR52)
1221
- is reset.
1222
-
1223
- FF16
1224
- Name - NR 21
1225
- Contents - Sound Mode 2 register, Sound Length; Wave Pattern Duty (R/W)
1226
-
1227
- Only bits 7-6 can be read.
1228
-
1229
- Bit 7-6 - Wave pattern duty
1230
- Bit 5-0 - Sound length data (t1: 0-63)
1231
-
1232
- Wave Duty: 00: 12.5% ( _--------_--------_-------- )
1233
- 01: 25% ( __-------__-------__------- )
1234
- 10: 50% ( ____-----____-----____----- ) (default)
1235
- 11: 75% ( ______---______---______--- )
1236
-
1237
- Sound Length = (64-t1)*(1/256) seconds
1238
-
1239
- FF17
1240
- Name - NR 22
1241
- Contents - Sound Mode 2 register, envelope (R/W)
1242
-
1243
- Bit 7-4 - Initial volume of envelope
1244
- Bit 3 - Envelope UP/DOWN
1245
- 0: Attenuate
1246
- 1: Amplify
1247
- Bit 2-0 - Number of envelope sweep (n: 0-7)
1248
- (If zero, stop envelope operation.)
1249
-
1250
- Initial volume of envelope is from 0 to $F.
1251
- Zero being no sound.
1252
-
1253
- Length of 1 step = n*(1/64) seconds
1254
-
1255
- FF18
1256
- Name - NR 23
1257
- Contents - Sound Mode 2 register, frequency lo data (W)
1258
-
1259
- Frequency's lower 8 bits of 11 bit data (x).
1260
- Next 3 bits are in NR 14 ($FF19).
1261
-
1262
- FF19
1263
- Name - NR 24
1264
- Contents - Sound Mode 2 register, frequency hi data (R/W)
1265
-
1266
- Only bit 6 can be read.
1267
-
1268
- Bit 7 - Initial (when set, sound restarts)
1269
- Bit 6 - Counter/consecutive selection
1270
- Bit 2-0 - Frequency's higher 3 bits (x)
1271
-
1272
- Frequency = 4194304/(32*(2048-x)) Hz
1273
- = 131072/(2048-x) Hz
1274
-
1275
- Counter/consecutive Selection
1276
- 0 = Regardless of the length data in NR21
1277
- sound can be produced consecutively.
1278
- 1 = Sound is generated during the time period
1279
- set by the length data in NR21. After this
1280
- period the sound 2 ON flag (bit 1 of NR52)
1281
- is reset.
1282
-
1283
- FF1A
1284
- Name - NR 30
1285
- Contents - Sound Mode 3 register, Sound on/off (R/W)
1286
-
1287
- Only bit 7 can be read
1288
-
1289
- Bit 7 - Sound OFF
1290
- 0: Sound 3 output stop
1291
- 1: Sound 3 output OK
1292
-
1293
- FF1B
1294
- Name - NR 31
1295
- Contents - Sound Mode 3 register, sound length (R/W)
1296
-
1297
- Bit 7-0 - Sound length (t1: 0 - 255)
1298
-
1299
- Sound Length = (256-t1)*(1/2) seconds
1300
-
1301
- FF1C
1302
- Name - NR 32
1303
- Contents - Sound Mode 3 register, Select output level (R/W)
1304
-
1305
- Only bits 6-5 can be read
1306
-
1307
- Bit 6-5 - Select output level
1308
- 00: Mute
1309
- 01: Produce Wave Pattern RAM Data as it is
1310
- (4 bit length)
1311
- 10: Produce Wave Pattern RAM data shifted once
1312
- to the RIGHT (1/2) (4 bit length)
1313
- 11: Produce Wave Pattern RAM data shifted twice
1314
- to the RIGHT (1/4) (4 bit length)
1315
-
1316
- * - Wave Pattern RAM is located from $FF30-$FF3f.
1317
-
1318
- FF1D
1319
- Name - NR 33
1320
- Contents - Sound Mode 3 register, frequency's lower data (W)
1321
-
1322
- Lower 8 bits of an 11 bit frequency (x).
1323
-
1324
- FF1E
1325
- Name - NR 34
1326
- Contents - Sound Mode 3 register, frequency's higher data (R/W)
1327
-
1328
- Only bit 6 can be read.
1329
-
1330
- Bit 7 - Initial (when set, sound restarts)
1331
- Bit 6 - Counter/consecutive flag
1332
- Bit 2-0 - Frequency's higher 3 bits (x).
1333
-
1334
- Frequency = 4194304/(64*(2048-x)) Hz
1335
- = 65536/(2048-x) Hz
1336
-
1337
- Counter/consecutive Selection
1338
- 0 = Regardless of the length data in NR31
1339
- sound can be produced consecutively.
1340
- 1 = Sound is generated during the time period
1341
- set by the length data in NR31. After this
1342
- period the sound 3 ON flag (bit 2 of NR52)
1343
- is reset.
1344
-
1345
- FF20
1346
- Name - NR 41
1347
- Contents - Sound Mode 4 register, sound length (R/W)
1348
-
1349
- Bit 5-0 - Sound length data (t1: 0-63)
1350
-
1351
- Sound Length = (64-t1)*(1/256) seconds
1352
-
1353
- FF21
1354
- Name - NR 42
1355
- Contents - Sound Mode 4 register, envelope (R/W)
1356
-
1357
- Bit 7-4 - Initial volume of envelope
1358
- Bit 3 - Envelope UP/DOWN
1359
- 0: Attenuate
1360
- 1: Amplify
1361
- Bit 2-0 - Number of envelope sweep (n: 0-7)
1362
- (If zero, stop envelope operation.)
1363
-
1364
- Initial volume of envelope is from 0 to $F.
1365
- Zero being no sound.
1366
-
1367
- Length of 1 step = n*(1/64) seconds
1368
-
1369
- FF22
1370
- Name - NR 43
1371
- Contents - Sound Mode 4 register, polynomial counter (R/W)
1372
-
1373
- Bit 7-4 - Selection of the shift clock frequency of the
1374
- polynomial counter
1375
- Bit 3 - Selection of the polynomial counter's step
1376
- Bit 2-0 - Selection of the dividing ratio of frequencies
1377
-
1378
- Selection of the dividing ratio of frequencies:
1379
- 000: f * 1/2^3 * 2
1380
- 001: f * 1/2^3 * 1
1381
- 010: f * 1/2^3 * 1/2
1382
- 011: f * 1/2^3 * 1/3
1383
- 100: f * 1/2^3 * 1/4
1384
- 101: f * 1/2^3 * 1/5
1385
- 110: f * 1/2^3 * 1/6
1386
- 111: f * 1/2^3 * 1/7 f = 4.194304 Mhz
1387
-
1388
- Selection of the polynomial counter step:
1389
- 0: 15 steps
1390
- 1: 7 steps
1391
-
1392
- Selection of the shift clock frequency of the polynomial
1393
- counter:
1394
-
1395
- 0000: dividing ratio of frequencies * 1/2
1396
- 0001: dividing ratio of frequencies * 1/2^2
1397
- 0010: dividing ratio of frequencies * 1/2^3
1398
- 0011: dividing ratio of frequencies * 1/2^4
1399
- : :
1400
- : :
1401
- : :
1402
- 0101: dividing ratio of frequencies * 1/2^14
1403
- 1110: prohibited code
1404
- 1111: prohibited code
1405
-
1406
- FF23
1407
- Name - NR 44
1408
- Contents - Sound Mode 4 register, counter/consecutive; inital (R/W)
1409
-
1410
- Only bit 6 can be read.
1411
-
1412
- Bit 7 - Initial (when set, sound restarts)
1413
- Bit 6 - Counter/consecutive selection
1414
-
1415
- Counter/consecutive Selection
1416
- 0 = Regardless of the length data in NR41
1417
- sound can be produced consecutively.
1418
- 1 = Sound is generated during the time period
1419
- set by the length data in NR41. After this
1420
- period the sound 4 ON flag (bit 3 of NR52)
1421
- is reset.
1422
-
1423
- FF24
1424
- Name - NR 50
1425
- Contents - Channel control / ON-OFF / Volume (R/W)
1426
-
1427
- Bit 7 - Vin->SO2 ON/OFF
1428
- Bit 6-4 - SO2 output level (volume) (# 0-7)
1429
- Bit 3 - Vin->SO1 ON/OFF
1430
- Bit 2-0 - SO1 output level (volume) (# 0-7)
1431
-
1432
- Vin->SO1 (Vin->SO2)
1433
-
1434
- By synthesizing the sound from sound 1
1435
- through 4, the voice input from Vin
1436
- terminal is put out.
1437
- 0: no output
1438
- 1: output OK
1439
-
1440
- FF25
1441
- Name - NR 51
1442
- Contents - Selection of Sound output terminal (R/W)
1443
-
1444
- Bit 7 - Output sound 4 to SO2 terminal
1445
- Bit 6 - Output sound 3 to SO2 terminal
1446
- Bit 5 - Output sound 2 to SO2 terminal
1447
- Bit 4 - Output sound 1 to SO2 terminal
1448
- Bit 3 - Output sound 4 to SO1 terminal
1449
- Bit 2 - Output sound 3 to SO1 terminal
1450
- Bit 1 - Output sound 2 to SO1 terminal
1451
- Bit 0 - Output sound 1 to SO1 terminal
1452
-
1453
- FF26
1454
- Name - NR 52 (Value at reset: $F1-GB, $F0-SGB)
1455
- Contents - Sound on/off (R/W)
1456
-
1457
- Bit 7 - All sound on/off
1458
- 0: stop all sound circuits
1459
- 1: operate all sound circuits
1460
- Bit 3 - Sound 4 ON flag
1461
- Bit 2 - Sound 3 ON flag
1462
- Bit 1 - Sound 2 ON flag
1463
- Bit 0 - Sound 1 ON flag
1464
-
1465
- Bits 0 - 3 of this register are meant to
1466
- be status bits to be read. Writing to these
1467
- bits does NOT enable/disable sound.
1468
-
1469
- If your GB programs don't use sound then
1470
- write $00 to this register to save 16% or
1471
- more on GB power consumption.
1472
- FF30 - FF3F
1473
- Name - Wave Pattern RAM
1474
- Contents - Waveform storage for arbitrary sound data
1475
-
1476
- This storage area holds 32 4-bit samples
1477
- that are played back upper 4 bits first.
1478
-
1479
- FF40
1480
- Name - LCDC (value $91 at reset)
1481
- Contents - LCD Control (R/W)
1482
-
1483
- Bit 7 - LCD Control Operation *
1484
- 0: Stop completely (no picture on screen)
1485
- 1: operation
1486
-
1487
- Bit 6 - Window Tile Map Display Select
1488
- 0: $9800-$9BFF
1489
- 1: $9C00-$9FFF
1490
-
1491
- Bit 5 - Window Display
1492
- 0: off
1493
- 1: on
1494
-
1495
- Bit 4 - BG & Window Tile Data Select
1496
- 0: $8800-$97FF
1497
- 1: $8000-$8FFF <- Same area as OBJ
1498
-
1499
- Bit 3 - BG Tile Map Display Select
1500
- 0: $9800-$9BFF
1501
- 1: $9C00-$9FFF
1502
-
1503
- Bit 2 - OBJ (Sprite) Size
1504
- 0: 8*8
1505
- 1: 8*16 (width*height)
1506
-
1507
- Bit 1 - OBJ (Sprite) Display
1508
- 0: off
1509
- 1: on
1510
-
1511
- Bit 0 - BG Display
1512
- 0: off
1513
- 1: on
1514
-
1515
- * - Stopping LCD operation (bit 7 from 1 to 0)
1516
- must be performed during V-blank to work
1517
- properly. V-blank can be confirmed when the
1518
- value of LY is greater than or equal to 144.
1519
-
1520
- FF41
1521
- Name - STAT
1522
- Contents - LCDC Status (R/W)
1523
-
1524
- Bits 6-3 - Interrupt Selection By LCDC Status
1525
-
1526
- Bit 6 - LYC=LY Coincidence (Selectable)
1527
- Bit 5 - Mode 10
1528
- Bit 4 - Mode 01
1529
- Bit 3 - Mode 00
1530
- 0: Non Selection
1531
- 1: Selection
1532
-
1533
- Bit 2 - Coincidence Flag
1534
- 0: LYC not equal to LCDC LY
1535
- 1: LYC = LCDC LY
1536
-
1537
- Bit 1-0 - Mode Flag
1538
- 00: During H-Blank
1539
- 01: During V-Blank
1540
- 10: During Searching OAM-RAM
1541
- 11: During Transfering Data to LCD Driver
1542
-
1543
- STAT shows the current status of the LCD controller.
1544
- Mode 00: When the flag is 00 it is the H-Blank period
1545
- and the CPU can access the display RAM
1546
- ($8000-$9FFF).
1547
-
1548
- Mode 01: When the flag is 01 it is the V-Blank period
1549
- and the CPU can access the display RAM
1550
- ($8000-$9FFF).
1551
-
1552
- Mode 10: When the flag is 10 then the OAM is being
1553
- used ($FE00-$FE9F). The CPU cannot access
1554
- the OAM during this period
1555
-
1556
- Mode 11: When the flag is 11 both the OAM and display
1557
- RAM are being used. The CPU cannot access
1558
- either during this period.
1559
-
1560
-
1561
- The following are typical when the display is enabled:
1562
-
1563
- Mode 0 000___000___000___000___000___000___000________________
1564
- Mode 1 _______________________________________11111111111111__
1565
- Mode 2 ___2_____2_____2_____2_____2_____2___________________2_
1566
- Mode 3 ____33____33____33____33____33____33__________________3
1567
-
1568
-
1569
- The Mode Flag goes through the values 0, 2,
1570
- and 3 at a cycle of about 109uS. 0 is present
1571
- about 48.6uS, 2 about 19uS, and 3 about 41uS. This
1572
- is interrupted every 16.6ms by the VBlank (1).
1573
- The mode flag stays set at 1 for about 1.08 ms.
1574
- (Mode 0 is present between 201-207 clks, 2 about
1575
- 77-83 clks, and 3 about 169-175 clks. A complete
1576
- cycle through these states takes 456 clks.
1577
- VBlank lasts 4560 clks. A complete screen refresh
1578
- occurs every 70224 clks.)
1579
-
1580
- FF42
1581
- Name - SCY
1582
- Contents - Scroll Y (R/W)
1583
-
1584
- 8 Bit value $00-$FF to scroll BG Y screen
1585
- position.
1586
-
1587
- FF43
1588
- Name - SCX
1589
- Contents - Scroll X (R/W)
1590
-
1591
- 8 Bit value $00-$FF to scroll BG X screen
1592
- position.
1593
-
1594
- FF44
1595
- Name - LY
1596
- Contents - LCDC Y-Coordinate (R)
1597
-
1598
- The LY indicates the vertical line to which
1599
- the present data is transferred to the LCD
1600
- Driver. The LY can take on any value between
1601
- 0 through 153. The values between 144 and 153
1602
- indicate the V-Blank period. Writing will
1603
- reset the counter.
1604
-
1605
- FF45
1606
- Name - LYC
1607
- Contents - LY Compare (R/W)
1608
-
1609
- The LYC compares itself with the LY. If the
1610
- values are the same it causes the STAT to set
1611
- the coincident flag.
1612
-
1613
- FF46
1614
- Name - DMA
1615
- Contents - DMA Transfer and Start Address (W)
1616
-
1617
- The DMA Transfer (40*28 bit) from internal ROM or RAM
1618
- ($0000-$F19F) to the OAM (address $FE00-$FE9F) can be
1619
- performed. It takes 160 microseconds for the transfer.
1620
-
1621
- 40*28 bit = #140 or #$8C. As you can see, it only
1622
- transfers $8C bytes of data. OAM data is $A0 bytes
1623
- long, from $0-$9F.
1624
-
1625
- But if you examine the OAM data you see that 4 bits are
1626
- not in use.
1627
-
1628
- 40*32 bit = #$A0, but since 4 bits for each OAM is not
1629
- used it's 40*28 bit.
1630
-
1631
- It transfers all the OAM data to OAM RAM.
1632
-
1633
- The DMA transfer start address can be designated every
1634
- $100 from address $0000-$F100. That means $0000, $0100,
1635
- $0200, $0300....
1636
-
1637
- As can be seen by looking at register $FF41 Sprite RAM
1638
- ($FE00 - $FE9F) is not always available. A simple routine
1639
- that many games use to write data to Sprite memory is shown
1640
- below. Since it copies data to the sprite RAM at the appro-
1641
- priate times it removes that responsibility from the main
1642
- program.
1643
- All of the memory space, except high ram ($FF80-$FFFE),
1644
- is not accessible during DMA. Because of this, the routine
1645
- below must be copied & executed in high ram. It is usually
1646
- called from a V-blank Interrupt.
1647
-
1648
- Example program:
1649
-
1650
- org $40
1651
- jp VBlank
1652
-
1653
- org $ff80
1654
- VBlank:
1655
- push af <- Save A reg & flags
1656
- ld a,BASE_ADRS <- transfer data from BASE_ADRS
1657
- ld ($ff46),a <- put A into DMA registers
1658
- ld a,28h <- loop length
1659
- Wait: <- We need to wait 160 microseconds.
1660
- dec a <- 4 cycles - decrease A by 1
1661
- jr nz,Wait <- 12 cycles - branch if Not Zero to Wait
1662
- pop af <- Restore A reg & flags
1663
- reti <- Return from interrupt
1664
-
1665
-
1666
- FF47
1667
- Name - BGP
1668
- Contents - BG & Window Palette Data (R/W)
1669
-
1670
- Bit 7-6 - Data for Dot Data 11 (Normally darkest color)
1671
- Bit 5-4 - Data for Dot Data 10
1672
- Bit 3-2 - Data for Dot Data 01
1673
- Bit 1-0 - Data for Dot Data 00 (Normally lightest color)
1674
-
1675
- This selects the shade of grays to use for
1676
- the background (BG) & window pixels. Since
1677
- each pixel uses 2 bits, the corresponding
1678
- shade will be selected from here.
1679
-
1680
- FF48
1681
- Name - OBP0
1682
- Contents - Object Palette 0 Data (R/W)
1683
-
1684
- This selects the colors for sprite palette 0.
1685
- It works exactly as BGP ($FF47) except each
1686
- each value of 0 is transparent.
1687
-
1688
- FF49
1689
- Name - OBP1
1690
- Contents - Object Palette 1 Data (R/W)
1691
-
1692
- This Selects the colors for sprite palette 1.
1693
- It works exactly as OBP0 ($FF48).
1694
- See BGP for details.
1695
-
1696
- FF4A
1697
- Name - WY
1698
- Contents - Window Y Position (R/W)
1699
-
1700
- 0 <= WY <= 143
1701
-
1702
- WY must be greater than or equal to 0 and
1703
- must be less than or equal to 143 for
1704
- window to be visible.
1705
-
1706
- FF4B
1707
- Name - WX
1708
- Contents - Window X Position (R/W)
1709
-
1710
- 0 <= WX <= 166
1711
-
1712
- WX must be greater than or equal to 0 and
1713
- must be less than or equal to 166 for
1714
- window to be visible.
1715
-
1716
- WX is offset from absolute screen coordinates
1717
- by 7. Setting the window to WX=7, WY=0 will
1718
- put the upper left corner of the window at
1719
- absolute screen coordinates 0,0.
1720
-
1721
-
1722
- Lets say WY = 70 and WX = 87.
1723
- The window would be positioned as so:
1724
-
1725
- 0 80 159
1726
- ______________________________________
1727
- 0 | |
1728
- | | |
1729
- | |
1730
- | Background Display |
1731
- | Here |
1732
- | |
1733
- | |
1734
- 70 | - +------------------|
1735
- | | 80,70 |
1736
- | | |
1737
- | | Window Display |
1738
- | | Here |
1739
- | | |
1740
- | | |
1741
- 143 |___________________|__________________|
1742
-
1743
-
1744
- OBJ Characters (Sprites) can still enter the
1745
- window. None of the window colors are
1746
- transparent so any background tiles under the
1747
- window are hidden.
1748
-
1749
- FFFF
1750
- Name - IE
1751
- Contents - Interrupt Enable (R/W)
1752
-
1753
- Bit 4: Transition from High to Low of Pin
1754
- number P10-P13.
1755
- Bit 3: Serial I/O transfer complete
1756
- Bit 2: Timer Overflow
1757
- Bit 1: LCDC (see STAT)
1758
- Bit 0: V-Blank
1759
-
1760
- 0: disable
1761
- 1: enable