Now that I have a bit more time, I can give a more comprehensive response.
First off, what are your goals? Speed, accuracy, and code presentation/maintainability are generally at odds with each other here. If you want a very simple system that just runs BASIC with the screen editor in text mode, that's a lot easier than trying to implement all the normal graphics modes, raster effects, run copy-protected games at full speed, etc. If you want to showcase your code readably, not just the runtime behavior, well... optimized code can be messy.
Quote:
- Does this make any sense? I doubt it somehow, but I fail to see how I should do it
It certainly does make sense, and would work for many simple cases.
Quote:
- If yes - is the precision enough to ever see some result with the SID or VIC?
Yes, if you write $d021 to change the background color to leave it at that new color, it doesn't really matter at which cycle it changed. The screen will stay at the new color, and that's what you want. The same applies to the SID; a few cycles shifted here or there won't cause any audible problems in regular use. Only advanced trickery used in games and demos would be problematic, though some simpler tricks like raster interrupts might jitter a lot more than the canonical case.
Quote:
- Probably I have to execute this Loop 1mio times a second, to get the 0.98 MHz of the 6502, right?
Not exactly. If the loop executes one instruction per iteration, and the average cycles taken per instruction is around 3, then that's ~330k iterations per second to keep up with realtime.
Quote:
About topic 2: If anybody has the time and nerves...I would be so thankful to get some "dummy-proof" Information about how the SID/VIC communicate with the CPU. Is it with Interrupts? All the Information I find in the Internet are way too complex for me to understand :/
It is a hardware topic, which can get quite complex if it's your introduction to such. I would suggest reading the 64doc and vic-ii articles linked above over and over as you understand more small pieces.
The VIC-II and SID (and CIAs, and RAM chips, etc) are mapped into the CPU's address space, meaning normal CPU reads & writes to addresses command those chips. Writes from the CPU to address $0103 will write to the RAM chips, while writes to to $d021 don't hit the RAM chips at all (in the standard configuration), they write to the VIC-II chip. Each major chip on the bus has a "Chip Select" pin which is activated when the read/write targets it. The PLA (configured by memory locations $00/01) determines which address should activate which chip. That activated chip then sees the address, whether it's a read or write, and if it's a write it also sees the data that the CPU put on the data bus lines.
A detailed example of what the hardware is doing:
CPU writes the value $0e to address $d021, by doing the following:
- CPU drives the 16-bit address bus to hold the value $d021
- CPU drives the 8-bit data bus to hold the value $0e
- CPU sets the R/W line to indicate Write
On every cycle, the PLA is also doing its thing to manage the bus:
- PLA examines the high bits of the address, the R/W line, and the current memory configuration (bits from the 6510's value at address $01 are hardwired to inputs on the PLA).
- PLA determines that this address in this configuration is addressing the VIC-II chip.
- PLA activates the CS (Chip Select) pin of the VIC-II chip for this clock cycle (and not the CS of any other chip, including the RAM chips).
The VIC-II chip notices an incoming write, since it was selected. Being wired to the bus, it directly reads the values of the address lines (=$d021) and the data lines (=$0e)
- The VIC-II puts this value of $0e into an on-chip register as indicated by the low-bits of the address, $21.
- Separately, as the VIC-II is drawing the screen and needs to render the background color, it refers to the current value in this internal register.
Reads are similar, except that the CPU indicates "Read" on the R/W line, and does not assert anything on the data bus. When one of the chips is selected while the R/W line indicates a Read, it will be the one to assert the data bus lines to the value that the CPU will then pick up.
Now, inside an emulator, all reads & writes need to figure out if they go to RAM or to a special chip. I believe these are all of them:
- SID
- VIC-II
- CIA1
- CIA2
- Character ROM
- BASIC ROM
- Kernal ROM
- Color RAM
- $00/01 data port inside the 6510
Reads from ROMs, and reads/writes against RAM are easy to do; just read/write a byte from an array representing the RAM or ROM.
Reading from special chips is often also a simple lookup, though some trigger behavior, like clearing an interrupt when a special address is read. When writing to many of the special chips, you can simply store a value in a register (like the VIC-II's background color). Then, when the VIC-II code runs, it can read that to draw colors; the act of writing that register doesn't have to perform any special graphics operations
in the simple case.
As mentioned above, it's reasonable to have separate functions per chip to perform reads or writes, to contain any special behaviors.
I know the C64 pretty well, not only the 6502 side of things, so feel free to ask anything.