News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_ajcasado

Chip8 emulator for Amstrad CPC

Started by ajcasado, 23:58, 12 November 24

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ajcasado

I've decided to share a Chip8 and Schip8 emulator I've been working on, even though there's still quite a bit left to finish it. It's a project I left on hold, and I'm not sure when (or if) I'll pick it back up. So if anyone wants to take it over, I'd be happy to help out!
The code is on GitHub: https://github.com/ajcasado/Chip8_CPC.
I've included a disk image with a few games—some of them work. The emulated keyboard maps to the keys 1,2,3,4, Q,W,E,R, A,S,D,F, Z,X,C,V. ENTER pauses the emulation, ESC restarts the game, and L opens the game load menu, where you can use the arrow keys to select a game and load it with ENTER.
I know it works on WinAPE, but it doesn't work on my CPC664.
CPC 664

Empiezas a envejecer cuando dejas de aprender.
You start to get old when you stop learning.

Gryzor

I'm assuming this is for the virtual machine, not the programming language?

ajcasado

Quote from: Gryzor on 08:25, 13 November 24I'm assuming this is for the virtual machine, not the programming language?
To be precise, it's an implementation of the virtual machine that executes the opcodes generated by assembling a CHIP-8/SCHIP-8 assembly program.
CPC 664

Empiezas a envejecer cuando dejas de aprender.
You start to get old when you stop learning.

ajcasado

Quote from: ajcasado on 09:07, 13 November 24To be precise, it's an implementation of the virtual machine that executes the opcodes generated by assembling a CHIP-8/SCHIP-8 assembly program.
To be honest, it's supposed to be, as there are some opcodes with incomplete implementation and others that are just outright broken.
CPC 664

Empiezas a envejecer cuando dejas de aprender.
You start to get old when you stop learning.

arnoldemu

#4
I think to make it work on your cpc664 change these lines:

from:
                    write_ay((7<<8)|0B11111000);

to

                    write_ay((7<<8)|0B00111000);

and

                    write_ay((7<<8)|0B11111111);

to
                    write_ay((7<<8)|0B00111111);

and:

write_ay((7<<8)|0B11111111);

to

write_ay((7<<8)|0B00111111);



bit 7 and 6 need to be 0.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Gryzor

Quote from: arnoldemu on 14:17, 13 November 24I think to make it work on your cpc664 change these lines:

from:
                    write_ay((7<<8)|0B11111000);

to

                    write_ay((7<<8)|0B00111000);

and

                    write_ay((7<<8)|0B11111111);

to
                    write_ay((7<<8)|0B00111111);

and:

write_ay((7<<8)|0B11111111);

to

write_ay((7<<8)|0B00111111);



bit 7 and 6 need to be 0.

Edited your post to add the code tag to avoid parsed emojis...

Prodatron

Cool project, I really like this kind of stuff!

GRAPHICAL Z80 MULTITASKING OPERATING SYSTEM

GUNHED

Very nice! Do you use firmware for printing Characters on screen?
http://futureos.de --> Get the revolutionary FutureOS (Update: 2024.10.27)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

ajcasado

No, I use the cpcrslib function cpc_PrintGphStrXYM1() from cpcrslib, which is installed via the CPC dev toolchain from @cpcitor 's GitHub repository. For disk access, I use the firmware, borrowing code from The Unofficial Amstrad WWW Resource and CPCmania.
CPC 664

Empiezas a envejecer cuando dejas de aprender.
You start to get old when you stop learning.

cpcitor

Hi! Nice project!

I've discovered the existence of CHIP8 and SCHIP8 on the HP-48 handheld calculator around 1991-1993.
I was thinking a few months ago that it might be nice to implement a CHIP8/SCHIP8 emulator on the CPC.
I'm happy to learn about your project.

Thanks for sharing, and also the disk with games. I've run Pong and was confused about the keys so I couldn't really play it -- I have managed to move both paddles down. As a coder I avoid as much as possible to assign any role to keys AZQW in any production because of variations in keyboard layout.

I've had a look at your code quickly. I see you've made many changes to code imported from https://github.com/AlfonsoJLuna/chip8swemu .

Quote from: ajcasado on 20:11, 13 November 24I use the cpcrslib function cpc_PrintGphStrXYM1() from cpcrslib, which is installed via the CPC dev toolchain from @cpcitor 's GitHub repository. For disk access, I use the firmware, borrowing code from The Unofficial Amstrad WWW Resource and CPCmania.

I have cloned your repo, run configure_cdtc_build_tree.sh then make run. I can compile into a DSK after removing cpcfirmware.h km_test_key.s km_time_please.s because they were redundant with each other and the function supplied by cpc-dev-tool-chain, and also made uint16_t screen_first_byte_address extern in one place because it was defined in two C files, though I'm not sure this is correct.
I haven't tried to put games on the disk image, so the program runs, lists the catalog, finds only itself in the disc, so shows only itself in the directory display.

Feel free to contact me about usage of cpc-dev-tool-chain. I have written a number of improvements and am cleaning up little by little so that they are stable enough to publish them.
Had a CPC since 1985, currently software dev professional, including embedded systems.

I made in 2013 the first CPC cross-dev environment that auto-installs C compiler and tools: cpc-dev-tool-chain: a portable toolchain for C/ASM development targetting CPC, later forked into CPCTelera.

ajcasado

#10
Quote from: cpcitor on 22:13, 13 November 24Hi! Nice project!
Hi! Thanks!!
Quote from: cpcitor on 22:13, 13 November 24I've discovered the existence of CHIP8 and SCHIP8 on the HP-48 handheld calculator around 1991-1993.
I actually discovered it the same way! I got myself an HP48SX in my first year of university (late 1991), and it's such a great tool for engineering. Between leaving my CPC at my parents' house and getting a 486 two years later, it was my only programmable gadget. I even used it to play games! At university, the infrared program-sharing scene was full of CHIP8, CHIP48, and SCHIP8 games—I spent countless hours playing Snake. I still have my HP48, though these days I use an emulator on Android for day-to-day stuff.

Quote from: cpcitor on 22:13, 13 November 24Thanks for sharing, and also the disk with games. I've run Pong and was confused about the keys so I couldn't really play it -- I have managed to move both paddles down. As a coder I avoid as much as possible to assign any role to keys AZQW in any production because of variations in keyboard layout.
The choice of keys is directly inherited from the keyboard of the original machine for which CHIP-8 was created, the Cosmac VIP. The emulated keyboard is a 4x4 matrix of keys. If you remember playing on the HP48, the keys were 7,8,9,+, 4,5,6,x, 1,2,3,-, 0,.,SPC,+. It's true that, with the full keyboard available on the CPC, this choice might seem a bit off. One of the features on the to-do list for the CPC implementation was to allow key remapping and even support for the joystick.
RCA Cosmac VIP
HP48sx

Quote from: cpcitor on 22:13, 13 November 24I've had a look at your code quickly. I see you've made many changes to code imported from https://github.com/AlfonsoJLuna/chip8swemu .
That's right, I started experimenting with that implementation to see if it was feasible to port it to the CPC, and it wasn't too much work. The first thing I changed was how the graphics were rendered: originally, they were drawn to a pixel-by-pixel buffer and then transferred to video memory pixel by pixel, which is super inefficient. On a modern machine, that's no big deal, but on the CPC, you need a different approach.

I ended up implementing a hardware double buffer for vertical software scrolling and screen clearing. Since I'm using mode 0 and the graphics are monochrome, I rotate the visible pen to clear the screen, which means only a small part needs erasing. However, this makes sprite rendering more complicated because the pen has to be switched constantly. I'm not sure if this approach is worth it in the end—I'd need to rewrite the sprite rendering routines in assembly to properly evaluate it.

I also tried to implement hardware horizontal scrolling, which has to be done in 4-pixel jumps. In mode 0 this is easily achieved by adjusting the video RAM offset using CRTC registers R12 and R13. However, I hit a wall when it came to calculating the coordinates for rendering, and you can see this issue in the game ANT.

For all this, this forum and the wiki have been my main inspiration, though I've also drawn from other sources, as I've mentioned throughout the code.
As for my programming style, I have to apologize for the lack of orthodoxy and for the fact that most of the comments are just notes to myself. If anyone needs clarification, don't hesitate to ask—I'm more than happy to help!

Quote from: cpcitor on 22:13, 13 November 24I have cloned your repo, run
Code Select Expand
configure_cdtc_build_tree.sh then
Code Select Expand
make run. I can compile into a DSK after removing
Code Select Expand
cpcfirmware.h km_test_key.s km_time_please.s because they were redundant with each other and the function supplied by cpc-dev-tool-chain, and also made
Code Select Expand
uint16_t screen_first_byte_addressCode Select Expand
extern in one place because it was defined in two C files, though I'm not sure this is correct.
You're absolutely right, I need to fix that in the GitHub repository.
Quote from: cpcitor on 22:13, 13 November 24I haven't tried to put games on the disk image, so the program runs, lists the catalog, finds only itself in the disc, so shows only itself in the directory display.
The compiled binary should work. What I usually do is take the AMSDOS-generated binary and add it to the DSK image with the ROMs using the fantastic iDSK. It's probably not the best method, though. Automating the creation of the disk image in the makefile with a selection of ROMs pulled from a list would definitely be helpful.
Quote from: cpcitor on 22:13, 13 November 24Feel free to contact me about usage of cpc-dev-tool-chain. I have written a number of improvements and am cleaning up little by little so that they are stable enough to publish them.
Thanks for the offer! I'll keep it in mind when I get back to the project. For now, FUZIX is taking up all the time I have for these kinds of things.
CPC 664

Empiezas a envejecer cuando dejas de aprender.
You start to get old when you stop learning.

cpcitor

Thanks for the extra explanations. I'm already impressed.

Quote from: ajcasado on 23:42, 13 November 24For now, FUZIX is taking up all the time I have for these kinds of things.

Are you ... porting FUZIX to the CPC?


I've considered playing with FUZIX or Zeal or others (though just as a daydream because most of my (limited) retro bandwidth is focused on writing a new "surprising" game, which goes along with cleaning up and publishing the improvement of cpc-dev-tool-chain that I wrote when writing Just Get 9).
Had a CPC since 1985, currently software dev professional, including embedded systems.

I made in 2013 the first CPC cross-dev environment that auto-installs C compiler and tools: cpc-dev-tool-chain: a portable toolchain for C/ASM development targetting CPC, later forked into CPCTelera.

ajcasado

#12
Quote from: cpcitor on 00:12, 14 November 24Are you ... porting FUZIX to the CPC?
Yes, and it's already usable!! 8) . You can find the details here.

https://www.cpcwiki.eu/forum/applications/port-of-fuzix-for-the-cpc6128/

Quote from: cpcitor on 00:12, 14 November 24I've considered playing with FUZIX or Zeal or others (though just as a daydream because most of my (limited) retro bandwidth is focused on writing a new "surprising" game, which goes along with cleaning up and publishing the improvement of cpc-dev-tool-chain that I wrote when writing Just Get 9).
I'm sure the surprise will be a good one!!
I encourage you to play around with FUZIX—I'm sure you can bring plenty of improvements to what I've managed so far. Both the super-useful cpc-dev-tool-chain and the super-addictive Just Get 9 are great proof of that. Congrats on both!
CPC 664

Empiezas a envejecer cuando dejas de aprender.
You start to get old when you stop learning.

cpcitor

Quote from: ajcasado on 00:27, 14 November 24I encourage you to play around with FUZIX—I'm sure you can bring plenty of improvements to what I've managed so far.

I'll try. Perhaps there are synergies.

Quote from: ajcasado on 00:27, 14 November 24Both the super-useful cpc-dev-tool-chain and the super-addictive Just Get 9 are great proof of that. Congrats on both!

Thanks for the kind words. Recenlty I've been considering adding feature of creating CP/M executables from your C/ASM project (mostly because it looks simple: add some header files, implementation of system calls, adjust crt0). I'll look into FUZIX as time permits. I'd be happy to contribute to your port!
Had a CPC since 1985, currently software dev professional, including embedded systems.

I made in 2013 the first CPC cross-dev environment that auto-installs C compiler and tools: cpc-dev-tool-chain: a portable toolchain for C/ASM development targetting CPC, later forked into CPCTelera.

Powered by SMFPacks Menu Editor Mod