Shop Learn
6502 CPU Emulator - Page 2 — Parallax Forums

6502 CPU Emulator

2»

Comments

  • roglohrogloh Posts: 3,252
    edited 2021-04-28 11:25

    @Wuerfel_21 said:
    Not every R/W, just the ones going to a tricky area. RAM and ROM usually sit at the top and bottom, so those cases can be cought early with a CMP+JMP each. C64 in particular has a weird memory map with lots of bank switching though. Still, the bottom of memory is always RAM on a 6502, for obvious reasons.

    Yeah some fast way to match the range of addresses is required. A CAM would be ideal but we don't have that so some sequential address testing combined with limited range checking are required unless you burn a 64kB block of HUB that tells you what to do for every possible address (the brute force approach). That's very wasteful however. There'll be a happy medium somewhere.

  • @rogloh said:

    @Wuerfel_21 said:
    Not every R/W, just the ones going to a tricky area. RAM and ROM usually sit at the top and bottom, so those cases can be cought early with a CMP+JMP each. C64 in particular has a weird memory map with lots of bank switching though. Still, the bottom of memory is always RAM on a 6502, for obvious reasons.

    Yeah some fast way to match the range of addresses is required. A CAM would be ideal but we don't have that so some sequential address testing combined with limited range checking are required unless you burn a 64kB block of HUB that tells you what to do for every possible address (the brute force approach). That's very wasteful however. There'll be a happy medium somewhere.

    Most systems tend to map stuff on page boundaries, so having a per-page table of function pointers could work. Doesn't even need to include the pages that are covered by the top/bottom CMPs

  • maccamacca Posts: 319

    Here is a more interactive demo running the Woz monitor.

    This means I built an Apple-1 ? :)

    If you don't know how to use it (like me), I found this site, where I also got the binaries:
    https://www.sbprojects.net/projects/apple1/wozmon.php

    Remember to use capital letters!

    Since I never used that thing before, and fighted a bit with the smartpins serial configuration, I hope to have got it right.
    The code also got some skip-pattern optimizations.

  • TonyB_TonyB_ Posts: 1,718

    Using XBYTE would produce the smallest and fastest 6502 emulator, without any doubt. XBYTE was designed to handle more complicated CPUs than the 6502.

    There is big scope for code saving, freeing up space for hardware emulation. For example, you don't need to duplicate the fetch code for every arithmetic or logical operation. Instead load a register with either (a) the address of the subroutine for the operation or (b) the skip pattern for the operation and combine code for similar operations in the same skip block.

    You will be amazed how much code disappears when using skipping as much as possible.

  • TonyB_TonyB_ Posts: 1,718

    How likely is it that the 6502 will fetch an instruction from $FFFF then wrap around to address $0000? If the answer is very unlikely or not at all, you could save more code and eliminate the PC variable.

    When you need to know PC, use the GETPTR instruction and subtract the base address. Note that a hidden GETPTR PB is done for each new bytecode and RFBYTE/RFWORD post-increment the hub pointer automatically.

  • maccamacca Posts: 319

    @TonyB_ said:
    There is big scope for code saving, freeing up space for hardware emulation. For example, you don't need to duplicate the fetch code for every arithmetic or logical operation. Instead load a register with either (a) the address of the subroutine for the operation or (b) the skip pattern for the operation and combine code for similar operations in the same skip block.

    You will be amazed how much code disappears when using skipping as much as possible.

    This is a work in progress, I tend to make things working before doing any optimization, and the skip pattern gave me a lot of trouble, that's why the initial posted code is as it is (the woz monitor demo already has some changes). I spent a lot of time trying to understand why an instruction wasn't working only to discover that I typed the pattern backward or that I have added an instruction and forgot to add the relative skip (or not-skip) bit in the pattern. One problem at a time please.

    I spend few hours today optimizing the skip patterns, one group of instructions at a time, with repeated tests to make sure nothing broke. This is a thing to do when you have a working code, otherwise it is a nightmare.

    How likely is it that the 6502 will fetch an instruction from $FFFF then wrap around to address $0000? If the answer is very unlikely or not at all, you could save more code and eliminate the PC variable.

    That was my excess of safety, only later when most of the code was already working that way, I realized that the 6502 has its reset and interrupt vectors at the top of the address space and any code that wraps around it is very likely broken anyway. I already removed that thing in my current development code.

    As said before, xbyte will be last thing to do, there are already a number of things to do.

  • TonyB_TonyB_ Posts: 1,718
    edited 2021-04-29 14:13

    I have written a program to generate skip patterns automatically. Without it my 8086 emulator would have been a nightmare.

    http://forums.parallax.com/discussion/comment/1513077/#Comment_1513077

  • roglohrogloh Posts: 3,252
    edited 2021-05-02 06:46

    @TonyB_ said:
    I have written a program to generate skip patterns automatically. Without it my 8086 emulator would have been a nightmare.

    http://forums.parallax.com/discussion/comment/1513077/#Comment_1513077

    Was that Z80 emulator (that I think your were doing a while back) also completed @TonyB_ ? I could have a use for that CPU if you plan to release that one to the public and if it is cycle accurate.

  • TonyB_TonyB_ Posts: 1,718
    edited 2021-05-02 10:37

    @rogloh said:

    @TonyB_ said:
    I have written a program to generate skip patterns automatically. Without it my 8086 emulator would have been a nightmare.

    http://forums.parallax.com/discussion/comment/1513077/#Comment_1513077

    Was that Z80 emulator (that I think your were doing a while back) also completed @TonyB_ ? I could have a use for that CPU if you plan to release that one to the public and if it is cycle accurate.

    I stopped all work on the Z80 when starting the 8086. I finished untested fast and slow (cycle-accurate) Z80 emulators before then, apart from the skip patterns. I need to modify my Z80 emulators to include auto-generated patterns and lessons learned from the 8086. The cycle-accurate Z80 in particular could have a lot more free space for system hardware emulation.

    One self-contained cog can emulate the Z80, without a doubt. The sheer quantity of instructions is the main issue. XBYTE was designed to cater for the Z80, to which I will return fairly soon, then decide about a public release. Emulator complexity for some popular CPUs in increasing order: 6502, 8080, Z80, 8086.

  • pik33pik33 Posts: 1,019

    .... M68000?

  • maccamacca Posts: 319

    Got a bit hooked with the Apple1 and went a little further with the emulation...

    https://youtu.be/gN8vVUQVLIM

    Sorry for the poor video quality.

  • cgraceycgracey Posts: 13,588

    @macca said:
    Got a bit hooked with the Apple1 and went a little further with the emulation...

    https://youtu.be/gN8vVUQVLIM

    Sorry for the poor video quality.

    Looks good, Macca. Was the Apple I monochrome?

  • maccamacca Posts: 319

    @cgracey said:
    Looks good, Macca. Was the Apple I monochrome?

    Yes, it was composite monochrome, the emulator outputs VGA (green instead of white because I liked more...).
    I want to add the composite output soon.

  • roglohrogloh Posts: 3,252
    edited 2021-05-04 05:56

    Nice work and nice monitor macca. I actually have the same one, a little Sony Trinitron 15 inch 100ES, which is perfect for emulator stuff with VGA. I noticed it dumped on a sidewalk around here for our annual hard garbage collection. Its VGA cable had been cut off but it was in pristine condition and I nabbed it before it rained and added a new cable. Works fine and is still in top condition. I now use it for my souped up Z80 Microbee system with P1 VGA output and it matches the whole 90's look I was going for at the time.

Sign In or Register to comment.