Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .--------------------------------------------------.
- ( P A R T 2 . M E M O R Y M A P O F T H E C 64 )
- `--------------------------------------------------´
- general contents: pastebin.com/hmpJmurr
- o==========o
- | Contents |
- o==========o
- A. General table
- B. How RAM is used by CPU and VIC
- C. VIC memory banks
- 1. General
- 2. Bank positions in RAM
- 3. Character ROM access
- D. I/O memory map
- X. References
- -<>-<>-<>-<>-<>-<>-<>-
- o==================o
- | A. General table |
- o==================o
- I refer to this table very often:
- =============================================================================
- Pos Size | Pos Pos | CPU (read & write) | VIC (default Bank 0),
- (kB) (kB) | (dec) (hex) | | r. only (hexadecimal)
- =============================================================================
- 0 2 | 0 0 | Zero pg., screen RAM, etc. | 0400 Scn RAM (1000 B)
- __________|_____________|____________________________| 07E7 _________________
- 2 38 | 2048 0800 | Free RAM | _________________
- | | (use for BASIC program) | 1000 Chr ROM (4 kB)
- | | | 2000 _________________
- | | |
- | | |
- 16 | | |_4000 _________________
- | | |
- | | |
- | | |
- | | |
- | | |
- | | |
- | | |
- | | |
- __________|_____________|____________________________|
- 40 8 | 40960 A000 | BASIC ROM |
- | | |
- | | |
- __________|_____________|____________________________|
- 48 4 | 49152 C000 | Upper free RAM |
- | | |
- __________|_____________|____________________________|
- 52 4 | 53248 D000 | I/O registers & color RAM | Col. RAM via
- | | (or character ROM) | separate bus
- __________|_____________|____________________________| (512 B)
- 56 8 | 57344 E000 | Kernal ROM |
- | | |
- 64 | 65535 FFFF | End |
- __________|_____________|____________________________|
- Some notes:
- - 1024 bytes = 1 kB = 400_hex B; 1000_hex B = 4 kB
- - The "Zero page" (0_dec - 255_dec) is addressable with one byte (it doesn't need a high byte) and therefore it's very fast to access.
- - A 191 byte tape buffer resides in the first kB of RAM from 828 to 1019_dec (033C - 03FB_hex). You can use it for a small assembly program.
- - Screen memory starts at 1 kB (400_hex). Try poke 1024,1. It should print an A in the upper left corner. Forty columns by 25 lines equals 1,000 characters, ending screen mem at 2023_dec. (also see screen color at 54k)
- - The first byte of the free BASIC RAM must contain a value of 0 so that BASIC program can be RUN, making free RAM size in decimal: 38911 (= 38k - 1). Recognize this number?
- - "The upper free 4 kB of RAM (from 48 kB to 52 kB) is such a tempting space for system additions that some applications compete for it. For example, the 'Universal Wedge DOS Support Program', that adds easy access to the disk communications channel, is usually loaded at 51 kB. Programs that use that part of RAM will therefore overwrite the 'DOS Support Program', with the result that they may not run correctly, or even at all. Likewise 'Simon's BASIC', the extended language which Commodore has released on cartridge, uses several locations in this range. Be aware when you use hardware additions that use this spot to hook into the system." (ref. [1], page 118).
- - Character ROM and I/O (D000_hex). Poke 53280,1 (= D020_hex) makes the border white. At 53k+455B the SID chip I/O instructions start.
- - Screen color (D800_hex = 55296_dec). Poke 55296,1 makes the first char on screen white. Forty columns by 25 lines equals 1,000. So 1,000 bytes are used for the colors of the characters on screen (also see Screen memory at 1k). This changes when hi-res graphics or sprites are used.
- - Kernal. Interesting locations for assembly: FFD2_hex = print on screen; FFE1_hex = run/stop; FFE4_hex = get keyboard input.
- o===================================o
- | B. How RAM is used by CPU and VIC |
- o===================================o
- Modern computers have internal memory (RAM) for the CPU to read programs from and separate video RAM for the video chip on the video card. The C64 does not. Both VIC and CPU use the same RAM. However, RAM is not discretely separated in a part for the VIC and a part for the CPU. The CPU reads it in "certain way" and the VIC in another. The PLA (Programmable Logic Array) chip manages that. Now, what does that mean?
- An example. If the CPU tries to read RAM from A000 to C000_hex then the PLA reroutes the CPU to the B.A.S.I.C. ROM chip. So the CPU thinks it's reading RAM when in fact it is reading ROM. This is called "shadowing": some RAM is hidden under the *shadow* of ROM chips.
- Why did Commodore do this? Why did they not simply place the ROM chips after the 64 kB of RAM to be read? Well, because the C64 has a 16 bit address bus which means is can only address 2^16 bytes = 64 kB. The RAM needs to be shadowed.
- Another example. When the VIC chip tries to read RAM from 1000 to 2000_hex then it is rerouted to the ROM chip that holds the 8x8 bitmaps of the characters (letters, numbers, etc.). This very same part of RAM is seen by the CPU as free memory to store a program in and read it from (and it does too!). No RAM is "lost" by shadowing.
- So some parts of RAM are "read twice": one chip may be rerouted to ROM and the other is free to read/write to it. Take for instance color RAM. It is a dedicated separate 512 kB chip on the C64's motherboard. The CPU writes to it by means of a re-route via D800 - DBE7_hex in RAM. The VIC does not read that part of RAM: it has a dedicated separate bus to the color RAM chip. Combined with the principle of a "memory bank" for the VIC this becomes a very difficult topic to understand and to form a clear picture of in one's mind. See the separate part about the VIC.
- Also notice that the part of RAM that is called "I/O Memory" for the CPU (D000 to E000_hex) is re-routed too. That is, the CPU does not actually write data in RAM when it tries to write data there. It is re-routed to, for instance, color RAM or the registers inside the VIC chip.
- To make things more complicated, the CPU re-routings by the PLA chip to one or more of the ROM chips can be set off (use register 0001_hex, see [3]). This is to enable the CPU to write data in RAM there, for instance, to be read by the VIC chip. After writing to RAM one may enable re-routing to ROM again. If one wants to write sprite data under Kernal ROM then it is important to re-enable shadowing. Don't forget to disable interrupts in Assembly code with the mnemonic SEI during the write process.
- One may even set the CPU's (rerouted) access to the I/O registers temporarily off (again, via 0001_hex). One may place data for the VIC chip (e.g. a bitmap) to be read there. Off course it is very important to enable access to the I/O registers again after the write.
- o=====================o
- | C. VIC memory banks |
- o=====================o
- Summary: set with DD00_hex (set write access to this register with DD02_hex)
- 1. General
- ~~~~~~~~~~
- The VIC-II chip can only see 16 kB (a 14 bit number) of memory. So the Commodore's RAM is divided up into 4 "banks" of 16 kB. The VIC can access the complete 64 kB of RAM because the 2 missing address bits are provided by one of the CIA I/O chips (they are the inverted bits 0 and 1 of port A of CIA 2: bits 0 - 1 of DD00_hex) [4]. That means one can select which bank to use with DD00_hex (appendix H in [6] appears to be wrong). Sometimes it may be necessary to set write access to this register with DD02_hex.
- 2. Bank positions in RAM
- ~~~~~~~~~~~~~~~~~~~~~~~~
- The VIC-II's memory banks:
- ======================================================
- Pos Size | Pos Pos | Contents | VIC-II
- (kB) (kB) | (dec) (hex) | for CPU |
- ======================================================
- 0 2 | 0 0 | Zero page | \
- __________|_____________|______________| |
- 2 38___| 2048 0800 | Free RAM |_____ |
- 4 4 | 1000 | | Char ROM img |- Bank 0
- ___| 2000 | |_____ | (default)
- | | | |
- | | | |
- | | | ______/
- | 4000 | | \
- | | | |
- | | | |
- | | | |- Bank 1
- | | | |
- | | | |
- | | | ______/
- | 8000 | | \
- ___| | |_____ |
- 4 | 9000 | | Char ROM img |
- __________|_____________|______________|_____ |- Bank 2
- 40 8 | 40960 A000 | BASIC ROM | |
- | | | |
- __________|_____________|______________| _______/
- 48 4 | 49152 C000 | Free RAM | \
- __________|_____________|______________| |
- 52 4 | 53248 D000 | IO/Char ROM | |
- __________|_____________|______________| |- Bank 3
- 56 8 | 57344 E000 | Kernal ROM | |
- | | | |
- 64k | 65535 FFFF | | |
- __________|_____________|______________|______________/
- The banks (in hexadecimal) start at:
- Bank 0: 0000_hex (default)
- Bank 1: 4000_hex
- Bank 2: 8000_hex
- Bank 3: C000_hex
- 3. Character ROM access
- ~~~~~~~~~~~~~~~~~~~~~~~
- If the CPU reads register 1000_hex in RAM then it reads a byte of code that the user programmed in BASIC or machine code. But if the VIC (in "bank 0 mode") reads that register then it is re-routed (shortcut) to the first byte of the 4 kB character ROM chip. If the VIC is in "bank 2 mode" then, instead, 9000 to A000_hex is rerouted to the character ROM.
- The 4 kB character ROM - or a "shadow image" of it - is in bank 0 and 2 at 1000 to 2000_hex relative from the start of the bank. Therefore one cannot place sprite data in RAM at 1000 - 2000_hex in bank 0 mode nor at 9000 - A000_hex in bank 2 mode if you want the VIC to read characters from the ROM chip. This is because both sprites and character bitmaps are read by the VIC-II chip and RAM from 1000 to 2000_hex is shortcut to the character ROM chip. However, one may happily place BASIC or machine language code at these RAM locations because they are not read by the VIC but by the CPU.
- It is possible to use a custom character set in RAM from another position in VIC banks 0 and 2 (instead of the standard ROM chip). In that case one can, of course, place sprite data etc. at 1000 or 9000_hex onwards. In the chapters "Character RAM" and "Bank config examples" of the doc "The VIC chip" and in the appendix "Use custom font" this is explained.
- One may also use the CPU to read Character ROM (at D000_hex). See paragraph "CPU: switch between I/O and char ROM" in the doc "The VIC chip" what that can be useful for.
- Character ROM is shadowed in the banks at:
- Bank 0: 1000_hex (in the 38 kB of free RAM)
- Bank 1: -none-
- Bank 2: 9000_hex (in the last 4 k of free 38 k RAM)
- Bank 3: -none- (D000_hex for the CPU)
- o===================o
- | D. I/O memory map |
- o===================o
- If the 4 kB of RAM from D000 to E000 for the CPU is set to point to I/O registers (default) then its mem map is like below [3].
- I/O mem for the CPU:
- E000 ___________ 4 kB
- | I/O 2 |
- DF00 |___________| 3¾ kB
- | I/O 1 |
- DE00 |___________| 3½ kB
- | CIA 2 |
- DD00 |___________| 3¼ kB
- | CIA 1 |
- DC00 |___________| 3 kB
- | Color | CPU write
- | RAM |<---------- & VIC read via
- D800 |___________| 2 kB separate bus
- | SID |
- | registers |
- D400 |___________| 1 kB
- | VIC |
- | registers |<---------- CPU access
- D000 |___________| 0 kB to VIC regs.
- These registers inside the VIC, CIA, SID etc. chips are directly written to by the CPU "via" the detour of D000 to E000_hex. So this RAM is free to use for, say, sprite data to be read by the VIC chip. For this one must set the interrupt temporarily off (assembly: SEI) and configure register $0001 to make RAM visible from D000 to E000_hex (see HTML mem map [3]).
- o===============o
- | X. References |
- o===============o
- [1] "Mapping the Commodore 64", Sheldon Leemon, 'Compute! Publications Inc.', ISBN 0-942386-23-X (1984).
- [2] Memory map by Jim Butterfield:
- www.atarimagazines.com/compute/issue29/394_1_COMMODORE_64_MEMORY_MAP.php
- [3] HTML C64 memory map by Joe Forster:
- https://sta.c64.org/cbm64mem.html
- [6] "The master memory map for the C64" by P. Pavelko and T. Kelly, Prentice Hall, ISBN 0-13-574351-6 (1983).
Add Comment
Please, Sign In to add comment