If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Lets make a Sega Genesis Emulator

I know I posted a topic about a Sega Genesis emulator before, but havent got a single response about anything on it. Almost as must as Nightwalkers useful post. Heck noone chills in the Games and Graphics area no more like the good ol days of VBForums. Anyways I'm going really off topic here. Lets freaking make a Sega Genesis emulator shall we. Yes this will be a very humerous learning experience in the way I'm typing this.

Ah the language of choice. Well I would love to do C++ but then noone would want to even touch it. At least in VBForums. I would love to make history pulling it off using VB6, which to some people is a dead language but continues to be widely used. Literally, worldwide. But I dont feel like making a thread on debating which language is better. So how bout we try both VB6 and VB.Net as 2 separate programs and see how far we get. Shall we? At least then we can compare in performance. Afterall, who would love to see "Blast Processing" in VB?!! Probably mostly none of you all because youre too busy posting questions on Winsock and SQL programing, and chilling in the VB.Net, VB6 and Chit Chat areas

For those who dont know or remember, there was a huge game console war back in the late 80s and early 90s between Nintendo and Sega. Nintendo had the NES which was an 8 bit system, and Sega had the Genesis which was 16 bit. It was always about bits. Noone really knew what bits meant. I mean what are bits anyways? As long as the graphics and games looked good, thats all that it really meant. Nintendo had really great titles and so did Sega. And this competition led to better games. Then the Super Nintendo came out in 1991 which was also 16 bit. It had better graphics and sound, and was in steady competition with Sega. However the Sega genesis, although its graphics were shallow in comparison due to the lack of colors, had a faster processer than the Super NES. This is when Sega had a huge ad campaign attacking Nintendo with the phrase "Blast Processing." And it worked in some aspects! Along with the phrase Genesis does what Nintendon't.

Whoh I got really off topic. Lets build the emulator. Ok to build the emulator it all starts with the processor. The Sega Genesis uses a Motorola 68000. The chip consist of 64 pins. However the number of opcodes according to my research is not known per say just yet due to the number of combinations. But the closest I've come is this document:

I know I'm getting way ahead of myself here but thats where it starts. And I have source code to implement for the instructions thankfully...just not in VB yet. So lets begin coding. We are going to need a basic emulation loop going before we put in the CPU. Now bare in mind, this is just a foundation to work with. It doesnt work on its own yet. So create a new project and add a module:

vb Code:

OptionExplicit

Public CPU_Paused AsBoolean

Public CPU_Running AsBoolean

Public Instruction(256)AsLong'Dont know how many instructions yet so keep it 256 for now

Public Cycles(256)AsLong

Public Opcode AsLong

Public PC AsLong

Public Tick_Count AsLong

PublicFunction ReadMemory(ByVal Address AsLong)AsLong

ReadMemory = 0

EndFunction

PublicSub Execute_68000()

If CPU_Paused = TrueThen

Do Until CPU_Paused = FalseAnd CPU_Running = True

DoEvents

Loop

EndIf

Opcode = ReadMemory(PC)' Fetch Next Operation

PC = PC + 1

Tick_Count = Tick_Count + Cycles(Opcode)

Select Case Instruction(Opcode)

End Select

DoEvents

EndSub

vb.net Code:

Option Explicit On

Option Strict On

Public Module mod68000

Public CPU_Paused AsBoolean

Public CPU_Running AsBoolean

Public Instruction(256)AsInteger'Dont know how many instructions yet so keep it 256 for now

Public Cycles(256)AsInteger''Dont know how many instructions yet so keep it 256 for now

Public Opcode AsInteger

Public PC AsInteger

Public Tick_Count AsInteger

PublicFunction ReadMemory(ByVal Address AsInteger)AsInteger

Return 0

EndFunction

PublicSub Execute_68000()

If CPU_Paused = TrueThen

Do Until CPU_Paused = FalseAnd CPU_Running = True

Application.DoEvents()

Loop

EndIf

Opcode = ReadMemory(PC)' Fetch Next Operation

PC = PC + 1

Tick_Count = Tick_Count + Cycles(Opcode)

SelectCase Instruction(Opcode)

EndSelect

Application.DoEvents()

EndSub

End Module

Thats all for now. Tomorrow we will actually start building the CPU from scratch one opcode at a time. And there are a lot! Have a good one

C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

Re: Lets make a Sega Genesis Emulator

The only thing you will need to know about the processor are the Opcodes and Address Modes. Once you understand what each instruction does, then you can code it. Shortly today when I have time, I'm gonna go about them

Re: Lets make a Sega Genesis Emulator

Wow after investigating 2 open source emulators written in C, one being a famous one called DGen, I noticed they both had nearly identical code with the 68k, only it was a lot more complex and not written like a simple NES emulator at all. It literally is working with binary numbers for the instruction set. But I don't want to just guess that it really is. I want to know. So were going to have to dig deeper. A simple google search of Sega Genesis Documentation led me to this page http://www.genny4ever.net/index.php?page=docs#md. So I'm gonna read up on the docs and quite possibly dig for more open source emulators to get a general idea on the 68k processor. The more we learn about it, the more we should be able to emulate it. Once we have a foundation to work with, then we can jump into things such as loading a game from file, reading and writing from memory, the PPU, APU, etc.

Re: Lets make a Sega Genesis Emulator

After reading that PDF, I gathered more information than anything. So what I'm gonna do is list the instructions. Then later we will logically program the instructions one by one as long as the instruction is associated with the CPU the Genesis uses. Bare in mind not all the instructions go with every M68000 processor as there are multiple versions of the M68000 family such as the MC68010, MC68020, MC68030, MC68040, as well as a CPU32 version. Each have similar instructions but have additional instructions the other processors don't have. What I want to know exactly is which processor does the model 1, 2, and 3 of the Sega Genesis uses, so I'm not wasting time coding on instructions that will never compute. For now I'm gonna list what I believe to be nearly all the instructions the entire M68000 family uses. So here is a list I gathered so far from that PDF found here: http://www.freescale.com/files/archi.../M68000PRM.pdf

C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

Re: Lets make a Sega Genesis Emulator

Honestly since I never made a Sega Emulator before I dont know. But Im passionate about emulation and very curious of the results since noone had the guts to even attempt it in VB. I have both the model 1 and 2 Sega Genesis at my house. So I am gonna take them apart to see what M68000 processor they use. Once I am satisfied, I can eliminate most of the instructions that wont be used.

Re: Lets make a Sega Genesis Emulator

I'm going to have to compare these to the 2 C++ version of the Sega Genesis emulator I currently have and do some research but at least I have a foundation for the M68000 processor's Opcodes source code to work with...in C#

Re: Lets make a Sega Genesis Emulator

Alrighty I'm back people. I took apart my Sega Genesis Model 1 and ran into a surprise. The Model 1 version I have has a 68 Pin Quad Pack Motorola MC68HC000FN8 Chip as you can see in these photos:

However I saw photos of another Genesis Model 1 that uses a 64 pin Motorola MC68000P8:

Don't exactly know the difference between them. Maybe it had something to do with the expansion port for the Sega CD, I honestly don't know. Matter of fact I found a good site showing the other chip sets Sega used: http://console5.com/wiki/68000

Bottom line is that I'm gonna assume that the Sega Genesis uses an MC68000 and nothing more. So with that said we are gonna eliminate the unused operands as of right now. So I'm gonna cycle through the PDF document I shown earlier and make a check list.

[EDIT] Its interesting to see that there were 3 different versions of the Sega Genesis Model 1. I have the latest version with the motherboard labeled PC BD M5 USA VA7. You can see it in the pic just under the heat sink. And here are the others: http://console5.com/wiki/Genesis

Re: Lets make a Sega Genesis Emulator

Ok so I weeded out the opcode instructions that the Sega Genesis doesn't have. All these instructions are part of the 68000 family according to the PDF. And the floating point instructions are NOT part of the MC68000. In fact the directly supported floating point instructions are only supported in the MC6888X and MC68040, and the indirectly supported floating point instructions are only supported in the MC6888X and MC68040FPSW. With X being I'm guessing version number. Bottom line is they arent supported at all in the Sega Genesis, hence, wont be calculated in the emulator. So now here is the new list of instructions I gathered up:

We narrowed it down to just 79 opcode instructions. You must bare in mind I'm taking all the information I'm gathering online and from other emulators with a grain of salt. If I see consistancy with any information, I'm gonna say its safe to assume its correct. Due to the fact that the PDF is the official Motorola 68000 chip documentation, I think I did a pretty good job. And just to see if I am truely right when I did narrow it down, there are a list of opcodes on another site which is here: http://emu-docs.org/CPU%2068k/68000inst.txt

C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

Re: Lets make a Sega Genesis Emulator

Nah its no different than how the Nintendo emulator was. Emulate the Opcode instructions, Read/Write from the Memory map only in this case its 32 bit so there are 16777215 addresses (or FFFFFF, see table)

and last but not least emulate the video and sound hardware. Its not rocket science. It just takes mad common sense skills

Re: Lets make a Sega Genesis Emulator

Since in the other emulators im not exactly seeing the ABCD instruction (probably because it was sort of embeded into the ADD instruction itself I'm assuming...at least in one of the emulators) we are going to skip that and focus on ADD. So according to the Motorola 68000 PDF documentation:

Code:

ADD
(M68000 Family)
Operation: Source + Destination→Destination
Assembler: ADD < ea > ,Dn
Syntax: ADD Dn, < ea >
Attributes: Size = (Byte, Word, Long)
Description: Adds the source operand to the destination operand using binary addition and stores the result in the destination location. The size of the operation may be specified as byte, word, or long. The mode of the instruction indicates which operand is the source and which is the destination, as well as the operand size.
Condition Codes:
X — Set the same as the carry bit.
N — Set if the result is negative; cleared otherwise.
Z — Set if the result is zero; cleared otherwise.
V — Set if an overflow is generated; cleared otherwise.
C — Set if a carry is generated; cleared otherwise.
Instruction Format:
X *
N *
Z *
V *
C *
|15|14|13|12|11|10|09|08|07|06|05|04|03||02|01|00|
| 1 | 1 | 0| 1 |REGISTER|OPMODE| MODE | REGISTER |

There are also multiple data types used for ADD such as Byte (8 bit), Short (16 Bit), as well as Integer (32 bit). 64 bit instructions are not supported. And according to one emulators I examined, there are 2 different versions of each. One version having condition code registers updated and the other not having condition code registers updated. For example, here is the C# version:

The C++ version unfortunately had waaaay too many versions of ADD and I believe was overkill and complex for no reason IMO. I think the main difference was the Program Counter (PC) and thats about it but I will need to examine the code more closely. So this will be the primary focus before we translate it to VB.

Re: Lets make a Sega Genesis Emulator

Ive been away for awhile since Ive been playing a lot of World of Warcraft lately. Rather than talk about one opcode at a time which would be slow and sluggish, Im doing the whole 68000 module. Once its complete in both VB6 and VB.Net you will be able to upload the game ROM and see the opcodes in action. Just no video or sound yet. Thatll be the next step.

Re: Lets make a Sega Genesis Emulator

Joined the forum just for this project after seeing your NES emulator thread leading here. I'm olaf, the programmer of olafnes and Zega Alpha (two emulators written in VB6). I'd like to hop on this Genesis project.

Re: Lets make a Sega Genesis Emulator

Welcome olaf! I saw your emulator olafnes some time ago, I was helping with jacob nes emulator, and researching more about basicnes when I found olafnes page on sourceforge. The basicnes 6502 emulation code was bad written, and had a lot of timing issues, wrong flags and messy code. I rewrote most of the code, based on virtuanes sources and docs from nesdev. Is a lot better now, just need some minor fixes. The wrong timing cause some strange glitches, like the map on battletoads intro, maniac mansion not scrolling correctly...

I VDP is similar to sega master system, then I think we can use the code vdp master system with some minor modifications.
Jacob, how it's going with the 68k CPU emulation? Need some help?

I've also written a completely working NES emulator in both C and FreeBASIC, as well as an Apple II emulator. I wrote all of the code in each of them, including the CPU stuff from scratch. So, I've emulated an 80186 (it has a few more instructions than the 8086) and a 6502. I can definitely help out. Here's a screenshot of my NES emu in action playing Mega Man 2:

Now let's do this, bitches! I really want to see a working Genesis emu in VB. Would be ridiculous! So where are we with this right now? Are you still on the m68k code? If you're stuck, I can help if you provide the current code.

Re: Lets make a Sega Genesis Emulator

Originally Posted by omundodogabriel

Welcome olaf! I saw your emulator olafnes some time ago, I was helping with jacob nes emulator, and researching more about basicnes when I found olafnes page on sourceforge. The basicnes 6502 emulation code was bad written, and had a lot of timing issues, wrong flags and messy code. I rewrote most of the code, based on virtuanes sources and docs from nesdev. Is a lot better now, just need some minor fixes. The wrong timing cause some strange glitches, like the map on battletoads intro, maniac mansion not scrolling correctly...

I VDP is similar to sega master system, then I think we can use the code vdp master system with some minor modifications.
Jacob, how it's going with the 68k CPU emulation? Need some help?

I'd love to see the fixes done to the source code if it's still around. I've been meaning to rework the emulator as I now have a lot more experience with that sort of thing after Zega Alpha. I've really gotten down and dirty with the Master System and Game Gear stuff in recent years, so I can carry that back to my NES project now. Zega Alpha also has the better Game Gear's VDP thanks to Parasyte (ROM hacker, discoverer of the Metroid code) and benryves (programmer of the Cogwheel emulator). I've never looked too much into the technical aspects of the Genesis VDP, but it's probable that the Game Gear's architecture is relatively closer to it. The increased color palette, sprite zooming, and so forth.

Most of my experience prior to working on emulators was application development. Much of what I added to the emulators I based my work off of was full screen, Game Genie, save states, movies, rewinding, PC controller support, memory editors, and so on. However I did fix things like timing issues, added support for peripherals, mappers, and things like that.

Might I suggest if we're welcome to work on the project to keep a shared FTP with the files on it or something? Perhaps also chatting on IRC as a group? I'd be more than happy to make some room on nothackers.com for the project.

Re: Lets make a Sega Genesis Emulator

I second an IRC channel. Great idea. It would definitely be the best way to work in realtime. Everything else is too slow, and it would take forever. I run an IRCd that could be used. Might be better than a full-blown public server for a project like this.

Re: Lets make a Sega Genesis Emulator

After doing some research on the Sega Genesis, I've found...

- The Sega Master System's Z80 processor is used as a co-processor for the Sega Genesis. So you're welcome to gut that out of Zega Alpha. This Z80 core comes from vbSpec, the Sinclair ZX Spectrum emulator written in VB6. This as well as the VDP and everything else is available to you guys from me.

- Wikipedia says that the Genesis VDP is actually derived from the Master System/Game Gear VDP. It's actually between them in number of possible colors and stuff, so I imagine they are very very close.

- The Texas Instruments SN76489 PSG sound chip is also used from the Master System. I don't have the FM chip also used programmed into my emulator (as only a few games supported it, mostly imports), but MEKA and Genesis emulators are open source so it'd be possible to bring that chip over too. In fact, if I can get a hold of vbt, I may be able to coax him into implementing that. He ported the PSG sound chip in from SMS Plus! for Saturn to VB6 for me.

Anyway, just figured I'd flesh out some more details that are obviously of major use. We could recode stuff as needed. Zega Alpha's source is available here. As far as I know my emulator passes the ZEXALL test ROM as well (an emulator tool to test how accurate your Z80 and VDP emulators are).

Re: Lets make a Sega Genesis Emulator

Hey guys, sorry about the long wait. I've been having some personal issues lately with my job, my gf (and her birthday), and other things. So I took a little break from VBForums and programming in general until I can get my life back in order. But I'm back now. Yes I will probably need some help and would love for you guys to join in on the project. I think with all of us as a team (including my awesome DirectX programming skills) we can add some "Blast Processing" in VB.

C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

Re: Lets make a Sega Genesis Emulator

Re: Lets make a Sega Genesis Emulator

The basic structure is pretty much still like a NES emulator I think. Just to play around with the idea, I used somebody's 68000 core in C and created a ROM loader for it, and started emulation with some debug output. I ran Sonic 1, and it runs code. It starts communicating with the VDP registers which sit at $C00000. So, if we started there and then began emulating the VDP's features I'm pretty sure we could at least get a Sonic title screen showing. Of course, we still need to make a 68000 core for VB first.

What I have here seems like it's waiting for some interrupts or the correct status flags from the VDP.

Re: Lets make a Sega Genesis Emulator

Notes on the Genesis VDP written by Charles MacDonald. I've chatted with him on many occasions and he's extremely knowledgeable about the Sega consoles and others. He's written SMS Plus, Genesis Plus, and TGEmu. To backup further what I was saying earlier about the VDP probably being based on the Master System and Game Gear's VDP (straight from his document):

Code:

----------------------------------------------------------------------------
1.) Overview
----------------------------------------------------------------------------
The Genesis VDP is derived from the Master System VDP, which in turn
was derived from the Texas Instruments TMS9918. As a result, the VDP is
programmed much like it's earlier counterparts.
Interestingly enough, none of the Sega produced VDP's are similar to the
later VDP models made by Yamaha, which were also based upon the TMS9918.
----------------------------------------------------------------------------
2.) Display Modes
----------------------------------------------------------------------------
To clarify naming conventions, here is a list of display modes used by
the various video chips mentioned:
Mode 0 - TMS9918 specific
Mode 1 - TMS9918 specific
Mode 2 - TMS9918 specific
Mode 3 - TMS9918 specific
Mode 4 - SMS mode
Mode 5 - Genesis mode
Supported mode list:
TMS9918 - Modes 0, 1, 2, 3
SMS - Modes 0, 1, 2, 3, 4
Genesis - Modes 4, 5 (possibly 0 and others as well)
If anybody has some information about how the Game Gear would fit into
this list, let me know. I assume it's identical to the SMS, but I don't
know about the TMS9918 compatability. (if any)

I'm sure there's other documentation around but I just felt like following up before I head to bed. So, the source code to my Master System/Game Gear emulator should actually be a pretty big help in the VDP area as well as sound and the Z80 co-processor.