ClioSport.net

Register a free account today to become a member!
Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

  • When you purchase through links on our site, we may earn an affiliate commission. Read more here.

Powered by insomnia, I started writing sonic the hedgehog



sn00p

ClioSport Club Member
  A blue one.
I have pretty bad insomnia, so I try to do something productive when it's at its worst.

back in the 8/16-bit days I wrote quite a few games, the last one I wrote was a r-type style game that I did with a friend which was written in assembler and ran under DOS (I had a 386 at the time, probably 256K of RAM).

For whatever reason that was the last game I wrote. I have good memories of that era of gaming, modern games do nothing for me but make me motion sick, so I've thought about writing and old school game many times, but when it comes down to actually doing it, I've never quite managed it.

Well, insomnia got back so I thought to myself "stop f**king about, just do it".

As I had no art or data and I was interested in refreshing the brain on the various techniques that are used for old platforms, I figured a good starting point was to replicate a bit of an old school game, use their assets and do enough to then start my own.

Of course, as I went down the rabbit hole I reached a point where I figured I might as well just do enough to get the first level completely working, I didn't want to use Mario because I don't think they're anywhere near technically impressive, they're basic and the mechanics are awful, and as the highlander said, there can be only one....and it's the game that when I saw it for the first time it blew my mind on just how impressive it was, especially as it was written by a single guy.

So, he's a nearly complete engine for the first GHZ level in Sonic, written from scratch in C++ using SDL for graphics & audio.

 

Jonnio

ClioSport Club Member
  Punto HGT Abarth
I agree Mario is scum. 👍😆👍

Pretty cool watching the gameplay with all the gubbins working away at the bottom.

Hope the insomnia eases off!
 

Matt Cup

ClioSport Club Member
  Leon Cupra
Incredible work, would have never known you’d have made this from scratch.
 

Ay Ay Ron

ClioSport Club Member
Amazing work.

We were a sega household but I was always jealous of people who had Mario.

After playing the original Mario and Sonic again, Sonic is the much better game.
 

sn00p

ClioSport Club Member
  A blue one.
Cool project, and really enjoyed following the progress (from one fellow insomniac and coder to another!) :ROFLMAO: I am tempted to start a thread on the Frogger project now! :ROFLMAO:
Do it, your frogger looks amazing

It was kind of fun just documenting it as I went along. both as an exercise in understanding how the megadrive hardware worked, but the kind of tricks Yugi Naka came up with to make this game work, and work as well as it did, it's still hugely impressive today considering he wrote it on his own with little support from anybody else.

I still think this game stands up today, much more than Mario of a similar age does, Nintendo must have flipped a switch when they first saw Sonic because it utterly pissed on Mario from a great height.
 

sn00p

ClioSport Club Member
  A blue one.
Amazing work.

We were a sega household but I was always jealous of people who had Mario.

After playing the original Mario and Sonic again, Sonic is the much better game.
Sonic is massively more impressive than Mario, both visually and technically.

Play Mario and sonic side by side today and you realise just how basic Mario actually is in comparison, you wouldn't think they're of the same era (snes Mario vs sonic)
 

sn00p

ClioSport Club Member
  A blue one.
This took about 2 months to get from zero to this, but a considerable amount of time was eaten up extracting the data from the original game and getting it into a format I could use.

I ended up writing a massive utility that extracted data from both the original ROM and also from the disassembly and creating PNG files for graphics and extracting all other game data into JSON files, the extraction code is spaghetti with lots of "ifdefs" to handle extracting parts out because the parsing and extraction is so convoluted that even for the same type of data, sometimes I had to slightly modify it for a specific file, so it's basically held together with ultra cheap sellotape.
 

sn00p

ClioSport Club Member
  A blue one.
I agree Mario is scum. 👍😆👍

Pretty cool watching the gameplay with all the gubbins working away at the bottom.

Hope the insomnia eases off!
I'll upload another vid in a bit, I have a whole load of other debug information that is visual that shows how the game actually works.

The megadrive is a pretty awesome bit of engineering, sonic really set a flag in the ground saying "this is what you can actually do", SEGA's output on the platform was pretty dire before sonic and most people don't realise that Sonic wasn't a release day title, it came along quite a bit after the console had been selling, once SEGA realised what they had, they bundled it into it and Sonic became to SEGA what Mario was to Nintendo. SEGA already had a "mascot" character before sonic, but obviously that changed.
 

sn00p

ClioSport Club Member
  A blue one.
So here's sonic "behind the scenes", this visually shows how sonic works, these are debug modes I built into the engine as I developed it because without it, doing stuff like this is utterly impossible.

Right at the start of the video there's a little pause while I try to remember what keys were what, but I start turning stuff on and off and you can see that as I do it.

Sonic has 2 "layers", the background which shows the mountains, water and clouds, the megadrive has support for 2 tile map layers so that's a physical limitation of the hardware. The foreground layer is more capable than the background layer.

In addition to the layer, the megadrive hardware has a set of registers that allow you to scroll the tile map, you can scroll the whole thing left, right, up and down or you can scroll individual lines. There's a weirdness between the western releases and Japanese releases, I implemented the clouds as they appear in the Japanese release, they scroll in 3 layers, the western release all clouds scroll together.

as the background progresses down, there's a couple of "bands" for the mountains and then the water scrolls on a line by line basis. The speed at which all these elements scroll is very carefully set in the original game and it was an utter c**t to replicate properly, but the result is that it produces an incredibly cool depth to the game and it literally comes for free as the hardware manages it.

The water (and waterfalls) shimmer, this is a trick that was common on old consoles where all the graphics use an indexed palette, there are a whole load of technical limitations because for a tile or sprite you use one row of the palette which means that you're limited to 15 colours + transparent, but different sprites can use different palettes so you can get quite colourful on screen.

Again, the water shimmering was a complete c**t to replicate, when I started the game I used SDL and all it actually does is draw sprites, but, that was a mistake because trying to replicate the line by line scrolling and the palette cycling is incredibly difficult to do at speed even on a modern computer, I had to disable hardware acceleration in SDL because it (quite rightly) wanted to use OpenGL/DirectX to draw the sprites on screen, which means they're in VRAM for fast drawing but it's very slow to update VRAM sprites on the fly, the result is if you run this with hardware acceleration on, the frame rate drops to 1 FPS, yes, the megadrive can beat a modern computer with a modern GPU.

I won't make that mistake again, next time I will create my own graphics engine with frame buffer, meaning I can do whatever operations I need to do by directly manipulating pixels, I can do that incredibly quickly on the PC and then I'll simply tell SDL to draw my buffer as one big sprite.

The palette cycling trick is also how the SEGA animation is done, believe it or not there's just a single image that doesn't change, the palette is cycling to create the light effect and then make it solid.

Back to the game....

So with the foreground and background layers off, I turn on the solid debug. The level is simply an array that contains tile indices, each tile has a corresponding graphic tile (i.e what you see) and an index to a "solid". Anywhere where there is terrain, there's a solid tile, so here you can see sprites and tiles only, sonic has "sensors" that are used to determine if he's hit something or whether he's standing on ground or not. He has 2 feet sensors that point down, they're positioned slightly to the left and right of centre (not to the extents of the sprites, even in the original game you can see this when standing near objects, the purple rock in GHZ is an example where he appears over the edges, they use a 3D effect to make it look like the rock has depth, it's incredibly subtle and very deliberate.

His ground sensors are always firing regardless of what he's doing, they ensure he stands on top of the ground and they make it possible for him to fall when he's not on solid ground.

There's a left and right sensor that are only active when he's moving in that direction, so that he stops when he hits a wall.

The solid information is very simple, it just contains enough information to describe the shape of the solidity of a tile, you can see this on screen when I switch stuff on and off, this solidity basically mirrors the graphic visually, so with just solids on you can see his world as how he interacts with it.

What you also see in this is that lots of stuff in the game isn't part of the tile map, anywhere you see a colourful graphic, that's a sprite. The megadrive has a load of limitations on sprites, you have a maximum of 80 per screen and that's further complicated by the fact that for any given scan line you can only display 8 sprites, if you try to draw more than that, then any sprites after the 8 will not be draw for that scan line.

This quirk is used on the title screen to make sonic appear above and below the banner, they purposefully put blank sprites on the screen to allow that to happen, it's real out of the box thinking. It's also used in the water levels to create a flickering effect.

It also causes visual errors on screen although they're hard to see, but if there are quite a lot of sprites on screen and sonic loses his rings, parts of rings won't be drawn as there are too many sprites per scan line, in reality you don't notice it, if you captured the screen though you would see this in action.

So things like the rope bridge and the collapsing ledges are not part of the tile map, they're drawn as sprites and each sprite object manages collisions with sonic, for objects and enemies the collision works the other way around, it's not sonic hitting an object (like it's sonic hitting the terrain), it's actually the object hitting sonic which makes sense for enemies, but it's less obvious for things like the collapsing terrain or bridges where logically you would think it's sonic that would be hitting the bridge.

this is necessary because each object has a different way in which it interacts with sonic, so the collision is done in reverse and when he does collide with something the object moves sonic or in the case of an object tells sonic he's standing on something, when sonic is standing on an object his ground sensors are inactive, it's the responsibility of the object to determine when he's not standing on it anymore and to reactivate his ground sensors so that he falls.

The different colours of solids determine how the solidity affects sonic, a red block is solid only from the top, meaning that if a red block is above sonic he can jump up and he will only collide with it when he's heading towards the ground, so this enables him to jump up and only platforms. Blue blocks are solid from all directions, meaning if he hits one from any direction he'll be stopped in his tracks.

One of the cleverest parts of the game is that sonic has 360 degrees of freedom, unlike Mario, sonic can run up walls. This is achieved with a really neat trick that basically comes for free...

He doesn't have 360 degrees of freedom! He lives in a very one dimensional world, but, the trick is that when he's on terrain that is >45, then they simply swap the sensors, his ground sensors instead of pointing down, now point right and it uses the wall collision instead of ground collision, this continues to change as he goes though all degrees, and this is what allows him to traverse the loops.

The loops are another neat trick because they need to be solid all the way around to allow him to enter, traverse and exit the loop, but if they're fully solid then he wouldn't be able to enter and exit, so the game uses a trick where it duplicates the entire level map but that particular section is diffeerent depending on which side of the loop he is, you can see when he runs around the loop as he passes the top the level map changes and the exit of the loop becomes avaialble, run the other way and the same thing happens.

There's a lot of information on how sonic works as people reverse engineered it, but this was not a trivial task getting to this point, things like the platforms are not described anywhere and I literally sat there for days trawling through the disassembly of the ROM figuring out how bits worked, but this has been an incredibly interesting and rewarding project, I've spent 30 years wondering just how they pulled this game off and even now I'm in awe of just what they did, and when I say they, I mean Yugi Naka, he wrote it all on his own without much corporate interest, SEGA didn't particularly care for what he was doing (and SEGA America were baffled by it and really had zero interest in it).

Miyamota (who's a designer and not a coder) is a well known name, but I bet nobody here had ever heard of Yugi Naka until I mentioned him, Miyamota hasn't got s**t on Naka.

 

SharkyUK

ClioSport Club Member
I was (still am) into consoles and games in a big way - both as a player and a developer. Like many here, I also spent many happy hours playing Sega titles back in the day, including Sonic of course. However... I have to admit that my fave game was Super Mario World on SNES. That still ranks as one of my top games ever, and maybe even in my top 2-3 games ever, too. Someone has to wave the flag for Nintendo... :ROFLMAO::p
 

sn00p

ClioSport Club Member
  A blue one.
What I love about Sonic is the sheer attention to detail, yes there are flaws/bugs with the logic bug you have to put this in the context of it being the early 90's, on hardware that often required "thinking out of the box" to get the impossible done, the special stage in sonic is one such example, although I don't particularly care for it, it's a pretty impressive feat on a console that can't rotate stuff and there's actually some pretty clever code behind it to create the illusion, Yuji Naka must have spent an inordinate amount of time on a section of the game that is tiny and basically a throw away - compared to the rest of the game.

It's much easier to see on the debug video above, but watch sonics hair when he's up to speed, it flaps in the wind, now imagine this on a CRT TV through an RF modulator in the 90's, I bet nobody noticed or had the ability to see it. There are other things in the game that you see that you probably don't pay too much attention to, one of these is a trick of the tile engine in the megadrive chipset, you essentially have the ability to say whether sprites appear above or below that tile, so as sonic runs along, he passes in front of and behind the trees, I never noticed this or rather my brain just accepted it and didn't really pay attention to it for 30 years until I did this project.

@SharkyUK and I have had many conversations about gaming from this era and before, before the mammoth development teams, sonic was essentially the Japanese version of the guys knocking out (fnar) games in their bedrooms for the ZX spectrum, I'm also incredibly jealous (in a good way) that he counts in his friends two of the most prolific game authors on the ZX spectrum who wrote one (well a series) of my top one or two speccy games.

There's something magical about these games, it might be partly rose tinted specs and it might been the ever fading accuracy of memories, but I still enjoy playing these games on and off, they have something that modern games don't (and I'm not talking colour clash, but yes, colour clash!).

I seriously hope sharky does a dev log of his frogger game because it looks stunning. My dev log I did to my Facebook and it was a really interesting experience and I had people comment just how much they enjoyed reading it, I tried to break as much stuff down on how stuff was done and tried to make it accessible to people who don't have any kind of understanding of coding.
 

sn00p

ClioSport Club Member
  A blue one.
I'm also a glutton for punishment, since the premise I used to start this was basically to get back into the mindset of the kind of tricks you use to write games on these systems, so the end game was always my own game.

Running stuff quickly on real hardware is useful for development, so as part of this whole project I have also started working on a "eprom emulator", I've had to design a few custom boards since I wanted to use high speed USB otherwise uploads to the RAM would be slow, so I ended up creating a board with just a processor, high speed USB and all IO broken out, along side another board that has a CPLD on it that manages the marshalling of signals from the console to to the emulated eprom, as I organised the hardware for speed, the console can't directly use the ram as it needs to be translated, hence the CPLD.

The two PCB's I designed are fully working revisions now, but as the actual interfacing to the console is somewhat in flux (I'm changing stuff as I go) that is currently prototyped on wire-wrap.....

I have absolutely no idea how many connections there are, it's at least several thousand and they all have to be in the correct place otherwise none of it works.



1669315295669.png

1669315248661.png
 
Last edited:

SharkyUK

ClioSport Club Member
I'm also incredibly jealous (in a good way) that he counts in his friends two of the most prolific game authors on the ZX spectrum who wrote one (well a series) of my top one or two speccy games.

Andrew and Philip send their regards! :ROFLMAO:(y)

(Christmas 1999, I think! Philip in the middle and me on the left). :ROFLMAO:

"The good old days..." of game development!

ISL-fancydress.png
 

sn00p

ClioSport Club Member
  A blue one.
B E A UTIFUL.

They're such legends, I honestly think I'd fangirl so much if I ever met them, might have to fire up TID in a bit!
 

sn00p

ClioSport Club Member
  A blue one.
f**king nerds 🤣

Cool project snoop, like the emulator/interface - you madman.
too f**king right!

The EPROM emulator is turning out to be a pig to debug, not because it's particularly difficult, but because my logic analyser only has 16 channels, and you can only buy 16 channel analysers for a reasonable price, once you jump to 32 to price gets silly and realistically, I need more than 32.

Essentially I'm trying to debug hardware blind to parts of what's going on, if I want to check out data I have to move wires across, if I'm after address lines I have to move wires back, if I want to see both, then I have to take a pick of a subset of each and then get horribly confused.

I have a cypress superspeed development board here which I could actually use to create a logic analyser with enough channels, but I really don't want to go down another tangent sub-project.
 

Touring_Rob

ClioSport Club Member
too f**king right!

The EPROM emulator is turning out to be a pig to debug, not because it's particularly difficult, but because my logic analyser only has 16 channels, and you can only buy 16 channel analysers for a reasonable price, once you jump to 32 to price gets silly and realistically, I need more than 32.

Essentially I'm trying to debug hardware blind to parts of what's going on, if I want to check out data I have to move wires across, if I'm after address lines I have to move wires back, if I want to see both, then I have to take a pick of a subset of each and then get horribly confused.

I have a cypress superspeed development board here which I could actually use to create a logic analyser with enough channels, but I really don't want to go down another tangent sub-project.
The 1000 soldered wires are hard to debug.... that is a surprise 🤣

PS - you never posted pictured of the door sign in the 3D printer thread 👀
 

sn00p

ClioSport Club Member
  A blue one.
The 1000 soldered wires are hard to debug.... that is a surprise 🤣

PS - you never posted pictured of the door sign in the 3D printer thread 👀

Don't mention the sign! I haven't yet got over the amount of stress that caused me.

The thing is, I'm somewhat of a perfectionist, I have a vision of how things should be and that's how they should be when it's finished, I can't even think about that particular project without breaking out into a cold sweat, because even after the last update, I literally went down a whole other different technique and I ended up printing the parts, creating silicone moles and then casting them in resin.

Why?

Because trying to smooth those FDM 3D printed parts was impossible, especially so when you're needing to put down a base coat that needs to be an ultra high shine as a base for paints over the top. Spraying onto a casted surface (be it injection molded plastics or resin casted) reduces the amount of effort required.

Even now I'm rocking back and forward thinking about it, I can't even bare to look at it because of the sheer amount of time, effort and experimentation it took.

I probably should have just bought a SLA printer since the parts would have come out smooth enough that I could have used them straight off.

I'm off to rock back and forward in the corner again now....
 


Top