Discuss Scratch

RokCoder
Scratcher
1000+ posts

BBC Micro Emulator (with games)

This studio and project are outdated. Please see the new forum.

https://scratch.mit.edu/discuss/topic/506597/



Home Computer - the full BBC Microcomputer emulator



https://scratch.mit.edu/projects/531881458/




Below is the old forum and links to the old project





Background information

BBC Micros were used in schools in the 1980's. I wanted to bring across an old game called ‘Thrust’ and, for some crazy reason, decided I might as well bring the entire computer across instead. I guess it's worked as you can now play Thrust exactly as it was originally played but inside of Scratch!

Games currently included
  • Snapper
  • Thrust
  • Monsters
  • Meteors
  • Chuckie Egg
  • Swoop
  • Arcadians
  • Dare Devil Dennis
  • Stryker's Run 2: Codename Droid
  • Elite
  • Castle Quest
  • Twin Kingdom Valley
What is fully implemented

The 6502 instruction set is fully implemented. I even had to implement some of the undocumented instructions because I found they were being used in some of the games that I've included. The cycle timings are accurate for all the opcodes but are not modified for slight variations due to crossing page boundaries and the like. The CRTC video controller is mostly implemented. This allows control of the display at a hardware level. Many projects alter the width and height of the visible screen and also use these registers for hardware scrolling (among other things). The ULA register is implemented to allow detection and use of the various graphics modes alongside dynamic palette changing. The system clock, user VIA and system VIA and various interrupt handlers are implemented. The full OS is included in the project. All OS calls are working (OSWRCH, OSBYTE, OSWORD, etc). Where applicable (or required for speed) I've intercepted calls to various specific OSWORD functions (e.g. for creating sound effects). VIA timers, user timers, vblank timing, inkey timers, keyboard buffers and other internal systems are implemented. Mode 7 teletext mode is implemented.

What isn't implemented at present

After implementing the main features and 6502 emulation I've largely worked by ensuring each system that is required by the projects I selected are fully working. At present you can't use the emulator as a computer for programming but it's very feasible to make it do so. If I pursue this then the emulator will be usable as a home computer and will also allow the execution of programs written in the BASIC programming language. At present it supports only projects written entirely in 6502 machine code. I haven't touched upon implementing tape or disc handling. This means that the supported projects must be single files. I've opted to intercept all sound calls to process them more natively for Scratch. The sounds don't sound very authentic at present but that's down to a bug in the Scratch handling of SetEffect and SetVolume - once these are sorted I have code ready to use authentic sound reproduction. If I choose to add more games then I will likely improve the emulation as I go - if something isn't working then I'll need to fix it!

Difficulties in getting the emulator working properly in Scratch

Speed! Emulating a system that runs at 4MHz and has hardware handling features is something slightly beyond what Scratch was designed for. As previously mentioned, there are bugs in the Scratch sound handling which means it can't emulate the sound correctly. It makes a go of it but has much better code already present for when the Scratch bugs have been fixed. The fact that Scratch runs at 30Hz is an issue but fortunately TurboWarp supports generic refresh speeds.

The built-in debugger

You'll see the debug button at the top left of the screen when looking at games in the emulator. If you tap that button you'll see the built-in debugger. I made extensive use of this for debugging the emulator while I was working on it.

Play - this shows a trace of the 6502 code as it is executing
Step - steps through execution one instruction at a time
Step 50 - steps through in steps of 50 instructions
Frame - steps through an entire frame (with the BBC Micro running at 50 frames per second)
Set BP - allows you to set a break point in the 6502. After hitting the play button, execution will stop when it reaches a breakpoint
Watch - display the value of a memory location (2 bytes) while stepping through or playing the project
Watch BP - allows a breakpoint to be set that is activated when the contents of the given memory location are updated
Clear - empties the log window. The window fills up extremely quickly. With 1000000+ lines it is going to start using a lot of resources!
Src+OS - toggles between including OS code. Any memory addresses above $8000 exist in the OS code
Analyse - sorts through the recorded log file to sort opcodes into order of frequency of use. This allowed me to concentrate optimisation efforts

Customising keyboard controls

The standard BBC keyboard controls weren't generally suited to modern keyboards so I added a sprite called project specific which contains script specific to each game. This mainly relates to keyboard control overrides. I've made it as easy as I can for people to implement their own controls in there if they're using a non-English keyboard. My suggestion would be to change that file as appropriate and then to remix. Or, even better, tell me the best keyboard mapping for different country's keyboards and I'll implement them into the project!

Last edited by RokCoder (May 18, 2021 22:50:27)

-SuperTurtle-
Scratcher
38 posts

BBC Micro Emulator (with games)

lol, the snaper game is a ripoff of PAC-MAN
or pac-man is a ripoff of snapper *shudder*
colinmacc
Scratcher
1000+ posts

BBC Micro Emulator (with games)

-SuperTurtle- wrote:

lol, the snaper game is a ripoff of PAC-MAN
or pac-man is a ripoff of snapper *shudder*

No you were right first time. Even in the 1980's copyright was a big deal…
s_federici
Scratcher
500+ posts

BBC Micro Emulator (with games)

This is extremely cool. How does it work? Where are the games stored?
RokCoder
Scratcher
1000+ posts

BBC Micro Emulator (with games)

s_federici wrote:

This is extremely cool. How does it work? Where are the games stored?
Thanks

s_federici wrote:

This is extremely cool. How does it work? Where are the games stored?
In the cpu sprite you'll find the real engine of the project. It works as any emulator would, by decoding the 6502 instructions and simulating the hardware. You'll get more of an idea if you hit the debug button and then tap the play button when you're playing a game in the emulator…

s_federici wrote:

This is extremely cool. How does it work? Where are the games stored?
If you look in the load sprite there are a number of variables in there containing BBC computer files in hex format.

I'll be adding more details in the first post as and when I have time.

Last edited by RokCoder (April 2, 2021 17:34:32)

kriblo
Scratcher
100+ posts

BBC Micro Emulator (with games)

Awesomely impressive work! It works perfectly on the titles I tested!
s_federici
Scratcher
500+ posts

BBC Micro Emulator (with games)

RokCoder wrote:

If you look in the load sprite there are a number of variables in there containing BBC computer files in hex format.

Thanks

RokCoder wrote:

I'll be adding more details in the first post as and when I have time.

I'll be eagerly waiting for it
colinmacc
Scratcher
1000+ posts

BBC Micro Emulator (with games)

OK so I've just spent the last hour playing Thrust, and I can't believe I'm playing a Scratch project - It plays perfectly!

I have no idea how you've done this! All it needs is a BASIC interpreter and you've effectively made a Beeb!

Minor bug report though, in your home-made instructions for Thrust, you say it's B to fire, when in fact it's Z to fire…


I wonder if the people on the Star Dot forums would be interesting in seeing this. Also @gtoal …
colinmacc
Scratcher
1000+ posts

BBC Micro Emulator (with games)

When I'm putting my name in the high score table, I'm able to use backspace….. how on earth? Is that something Turbowarp does?
RokCoder
Scratcher
1000+ posts

BBC Micro Emulator (with games)

colinmacc wrote:

OK so I've just spent the last hour playing Thrust, and I can't believe I'm playing a Scratch project - It plays perfectly!

I have no idea how you've done this! All it needs is a BASIC interpreter and you've effectively made a Beeb!

Minor bug report though, in your home-made instructions for Thrust, you say it's B to fire, when in fact it's Z to fire…


I wonder if the people on the Star Dot forums would be interesting in seeing this. Also @gtoal …

I'm really glad you're enjoying it! The BASIC interpreter is actually in there with the OS and might not be too far off working. In theory I could call the OS routine that's triggered when you turn the computer on and see what happens. I might try that at some point but it feels like a rabbit hole just waiting for me to jump in! I've put a post on StarDot as I got some help from those guys in tracing undocumented use of SHEILA registers. That was in Castle Quest which also uses illegal opcodes. Sneaky, sneaky!

Also, bug fixed! Thanks for pointing it out.

colinmacc wrote:

When I'm putting my name in the high score table, I'm able to use backspace….. how on earth? Is that something Turbowarp does?

I was pleasantly surprised when I found this out. You can also use shift, insert, delete, etc!

Last edited by RokCoder (April 3, 2021 04:28:44)

RokCoder
Scratcher
1000+ posts

BBC Micro Emulator (with games)

kriblo wrote:

Awesomely impressive work! It works perfectly on the titles I tested!

Thanks! I'm fighting the urge to add more. Must… walk… away…
RokCoder
Scratcher
1000+ posts

BBC Micro Emulator (with games)

s_federici wrote:

RokCoder wrote:

If you look in the load sprite there are a number of variables in there containing BBC computer files in hex format.

Thanks

RokCoder wrote:

I'll be adding more details in the first post as and when I have time.

I'll be eagerly waiting for it

It's updated. I can't see many people wading through that information so I probably won't expand upon it any further!

I just had a thought about the problem you mentioned with the keyboard mappings not being ideal (or even usable) on an Italian keyboard. If you let me know what keys would be better for any (or all) of the projects then I can use the Scratch language block to automatically use those keys for Italian keyboards!
PutneyCat
Scratcher
500+ posts

BBC Micro Emulator (with games)

As per my comment on the project itself, congratulations - really seems like a monumental achievement! Like s_federici, I'm struggling a bit to understand how it works, so a couple of dumb questions follow… I vaguely grasp that you have emulated the BBC micro itself, so that you can play games by loading them. I've located what seem to be long variables in the load sprite with names referring to the games. Does this mean the code for each game (as opposed to the system the code runs on) is short enough to fit into a single variable? And how do you get the information/code you need to do something like this - is the source code for the BBC micro publicly available to anyone? Anyway, well done again!
RokCoder
Scratcher
1000+ posts

BBC Micro Emulator (with games)

PutneyCat wrote:

As per my comment on the project itself, congratulations - really seems like a monumental achievement! Like s_federici, I'm struggling a bit to understand how it works, so a couple of dumb questions follow… I vaguely grasp that you have emulated the BBC micro itself, so that you can play games by loading them. I've located what seem to be long variables in the load sprite with names referring to the games. Does this mean the code for each game (as opposed to the system the code runs on) is short enough to fit into a single variable? And how do you get the information/code you need to do something like this - is the source code for the BBC micro publicly available to anyone? Anyway, well done again!

You've found the games but not the OS. There's actually a variable in the BBC Micro sprite called _mem.fullDump which contains a full dump of the 64K (ROM and RAM combined) in the BBC Micro which is needed to allow the emulation to work. Technically I only needed the 32K ROM which would initialise all of the RAM but I've tried to keep things sane for Scratch so it doesn't go through the full OS initialisation process. You can fit pretty much anything into a single sprite in Scratch. It's way more efficient to use this method than to have pre-initialised lists.

There's a thriving BBC Micro community out there and pretty much anything is downloadable. I also have a BBC Micro which I could have grabbed the images from. I cheated though, fired up the BeebEm emulator and exported the memory dump from there. Simply saved the entire dump to a file within the emulator, exported it to outside of the emulator, dragged it into a File2Hex app and job done.

Amazingly I did find source code for most of the OS which was great for debugging purposes but not really essential in terms of getting the emulator working.

I'm happy to answer questions about this all day long. It was a real blast back to my childhood digging into the internals of the BBC Micro. Feel free to fire any questions you have this way!
PutneyCat
Scratcher
500+ posts

BBC Micro Emulator (with games)

Thanks - very interesting!
s_federici
Scratcher
500+ posts

BBC Micro Emulator (with games)

RokCoder wrote:

It's updated. I can't see many people wading through that information so I probably won't expand upon it any further!

Thanks a lot. I'm involved in explaining CS to people that get quickly frustrated when they see “raw” programming environments. So having a fully working OS and interpreter is a real gift.

RokCoder wrote:

I just had a thought about the problem you mentioned with the keyboard mappings not being ideal (or even usable) on an Italian keyboard. If you let me know what keys would be better for any (or all) of the projects then I can use the Scratch language block to automatically use those keys for Italian keyboards!

Great idea! The keys that are difficult to use in an Italian keyboard are

@ (you need ALT)
? (you need SHIFT)
< and > (they are on the same key)

Also Ins / Del can be a problem if you are using a portable Mac keyboard.

Every other arrow or alphabetical/numerical key would be ok.
RokCoder
Scratcher
1000+ posts

BBC Micro Emulator (with games)

s_federici wrote:

RokCoder wrote:

I just had a thought about the problem you mentioned with the keyboard mappings not being ideal (or even usable) on an Italian keyboard. If you let me know what keys would be better for any (or all) of the projects then I can use the Scratch language block to automatically use those keys for Italian keyboards!

Great idea! The keys that are difficult to use in an Italian keyboard are

@ (you need ALT)
? (you need SHIFT)
< and > (they are on the same key)

Also Ins / Del can be a problem if you are using a portable Mac keyboard.

Every other arrow or alphabetical/numerical key would be ok.

I think I've sorted it. Should now be localised for UK and Italian keyboards and a piece of cake to tweak or modify for others. If there are still errant keys then please let me know.
Litu-1
Scratcher
100+ posts

BBC Micro Emulator (with games)

I love it

Last edited by Harakou (April 4, 2021 07:37:43)

Chiroyce
Scratcher
1000+ posts

BBC Micro Emulator (with games)

The logo at the top-right corner doesn't load



Console:

Litu-1
Scratcher
100+ posts

BBC Micro Emulator (with games)

Chiroyce wrote:

The logo at the top-right corner doesn't load



Console:


How many times have you tried?

Powered by DjangoBB