Scott Hanselman

Emulating a PlayStation 1 (PSX) entirely with C# and .NET

September 12, '19 Comments [15] Posted in Gaming | Open Source
Sponsored By

I was reading an older post in an emulator forum where someone was asking for a Playstation 1 (PSX) emulator written in C#, and the replies went on and on about how C# and .NET are not suited for emulation, C# is far too slow, negativity, blah blah.

Of course, that's silly. Good C# can run at near-native speed given all the work happening in the runtime/JITter, etc.

I then stumbled on this very early version of a PSX Emulator in C#. Now, if you were to theoretically have a Playtation SCPH1001.BIN BIOS and then physically owned a Playstation (as I do) and then created a BIN file from your physical copy of Crash Bandicoot, you could happily run it as you can see in the screenshot below.

Crash Bandicoot on a C#-based PSX Emulator

This project is very early days, as the author points out, but I was able to Git Clone and directly open the code in Visual Studio 2019 Community (which is free) and run it immediately. Note that as of the time of this blog post, the BIOS location *and* BIN files are hardcoded in the CD.cs and BUS.cs files. I named the BIN file "somegame.bin."

PSX Emualtor in C# inside Visual Studio

A funny note, since the code is unbounded as it currently sits, while I get about 30fps in Debug mode, in Release mode the ProjectPSX Emulator runs at over 120fps on my system, emulating a PlayStation 1 at over 220% of the usual CPU speed!

Just to make sure there's no confusion, and to support the author I want to repeat this question and answer here:

Can i use this emulator to play?

"Yes you can, but you shouldn't. There are a lot of other more capable emulators out there. This is a work in progress personal project with the aim to learn about emulators and hardware implementation. It can and will break during emulation as there are a lot of unimplemented hardware features."

This is a great codebase to learn from and read - maybe even support with your own Issues and PRs if the author is willing, but as they point out, it's neither complete nor ready for consumption.

Again, from the author who has other interesting emulators you can read:

I started doing a Java Chip8 and a C# Intel 8080 CPU (used on the classic arcade Space Invaders). Some later i did Nintendo Gameboy. I wanted to keep forward to do some 3D so i ended with the PSX as it had a good library of games...

Very cool stuff! Reading emulator code is a great way to not only learn about a specific language but also to learn 'the full stack.' We often hear Full Stack in the context of a complete distributed web application, but for many the stack goes down to the metal. This emulator literally boots up from the real BIOS of a Playstation and emulates the MIPS R3000A, a BUS to connect components, the CPU, the CD-ROM, and display.

An emulator has to lie at every step so that when an instruction is reached it can make everyone involved truly believe they are really running on a Playstation. If it does its job, no one suspects! That's why it's so interesting.

You can also press TAB to see the VRAM visualized as well as textures and color lookup tables which is super interesting!

Visualizing VRAM

One day, some day, there will be no physical hardware in existence for some of these old/classic consoles. Even today, lots of people play games for NES and SNES on a Nintendo Switch and may never see or touch the original hardware. It's important to support emulation development and sites like archive.org with Donations to make sure that history is preserved!

NOTE: It's also worth pointing out that it took me about 15 minutes to port this from .NET Framework 4.7.2 to .NET Core 3.0. More on this, perhaps, in another post. I'll also do a benchmark and see if it's faster.

I encourage you to go give a Github Star to ProjectPSX and enjoy reading this interesting bit of code. You can also read about the PSX Hardware written by Martin Korth for a trove of knowledge.


Sponsor: Develop Xamarin applications without difficulty with the latest JetBrains Rider: Xcode integration, JetBrains Xamarin SDK, and manage the required SDKs for Android development, all right from the IDE. Get it today

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Monday, September 16, 2019 1:29:57 AM UTC
Pretty neat, was not aware of this emulator. Reminds me of the C# Quake 3 "port" back in the .NET 1.1 days quelling the "C# isn't fast enough" qualms.

Definitely second the deep dive into reading emulator code to learn a ton in a short period of time.
Jarred Capellman
Monday, September 16, 2019 3:27:35 AM UTC
Have you considered making a PR to contribute the .NET Core switch back to the main project? Would it be multiplatform at that point?
Monday, September 16, 2019 3:35:20 AM UTC
Did you send a diff, or create a public fork for your uplift to .Net Core 3? Also, does this work cross platform? Looks like winforms, and unsure of mono forms works, or could switch to eto forms as replacement options.
Michael J. Ryan
Monday, September 16, 2019 3:56:53 AM UTC
I was reading an older post in an emulator forum where someone was asking for a Playstation 1 (PSX) emulator written in C#
Why? This is the first time I hear that someone cares about the programming language of a piece of software.
FleetCommand
Monday, September 16, 2019 4:07:50 AM UTC
Haven’t done a diff yet. Maybe when 3.0 is released.

No, it won’t make it cross platform, but it COULD if the winForms dependency is removed. Remember that Core 3.0 *can* call OS-specific APIs, and this is a Windows App, even though I moved it to Core 3.0.
Monday, September 16, 2019 10:13:41 AM UTC
This is very cool. In guess, in theory, the WinForms dependency could be swapped out for something like AvaloniaUI?
John
Monday, September 16, 2019 3:55:31 PM UTC
I find it so funny that people would say it’s not possible to emulate things with .NET. I’ve recently for fun written emulators for GameBoy and NES (not perfect, but very functional) in .NET Core 3 for fun. Also C64 emulator that does many things. I already wrote an iPaw 3630 emulator years ago that almost boots Linux on a serial terminal.

It’s fun and it’s nice with C#. But of course some things are a lot easier with lower level languages.
Sami Kuhmonen
Tuesday, September 17, 2019 9:57:35 AM UTC
What a fantastic project
Tuesday, September 17, 2019 4:41:49 PM UTC
Hmm I wonder if we port the display code to use Xamarin Forms instead of winforms would that fix things (it’d be able to run on mobile, Linux, mac, and Windows)
Wednesday, September 18, 2019 9:21:38 AM UTC
Superb project. I was not aware of this. Gave a star on github.
Wednesday, September 18, 2019 1:57:28 PM UTC
Everyone who think that "C# is not fast enough" should read this
Wednesday, September 18, 2019 4:09:40 PM UTC
Still looking for a comprehensive list of .NET framework API calls missing, broken, no upgrade path in .NET core.

An upgrade path from .NET Framework to .NET core should exist without requiring a rewrite of code using primary .NET Framework APIs such as image handling, SMTP email server integration, HTTPContext, and using .NET core to build a WCF server.
Ted
Thursday, September 19, 2019 1:45:39 PM UTC
There is a project on github which aims to do the same, but then for the PS2/PSP: https://github.com/Xirexel/OmegaRed
Roel
Monday, September 23, 2019 2:59:12 PM UTC
It seems to be the biggest project and it can't be easy to do it although C# is used in it. Read More
Cynthia
Tuesday, September 24, 2019 8:18:40 PM UTC
Very cool. I recently went out to Wikipedia to see what was happening in the world of gaming engines, specifically to see which languages were most widely adopted in the genera. C# is very common in game development, in particular this engine looks really interesting: http://xenko.com/
Jessica
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.