Scott Hanselman

The Weekly Source Code 44 - Virtu, an Apple Emulator in C# for Silverlight, WPF and XNA

July 14, '09 Comments [8] Posted in Arcade | Open Source | Silverlight | Source Code | Windows Client | WPF
Sponsored By

Virtu.RasterBlaster I really advocate folks reading as much source as they can because you become a better writer by reading as much as writing. That's the whole point of the Weekly Source Code - reading code to be a better developer.

Reading code in Open Source projects is a good way to learn, especially if the project has been around a while and been successful, or if you already respect the team of people working on it. Less reliably, you can find snippets of code by searching and sharing code.

I love Emulators. They are magical. Earlier this year I interviewed Pete Brown when he created a C64 Emulator in Silverlight.

Now, it's Apple IIe time. From the Virtu Project Site, you can see that this source has been around in various forms for years...morphing from form to form.

Originally developed for RISC OS (3.11) on the Acorn Archimedes in 1995 using some C but mostly ARM assembly language. Published on the cover disk of the October 1997 issue of Acorn User. Later that year we started porting Virtu to Microsoft Windows (95) on the 'PC' using only C++ with DirectX. A port to Microsoft Windows CE (2.11) soon followed. These were tweaked over the next couple of years but never published. Fast forward to the present and the latest incarnation of Virtu, this time ported to the Microsoft .NET Framework (3.5 SP 1) using only C# with Silverlight, WPF and XNA (on both Windows and Xbox 360, which is limited to the .NET Compact Framework).

In this form, Virtu was written by Sean Fausett with some help from Nick Westgate. This code is interesting for a number of reasons. First, because it's a freaking AppleIIe emulator in a language I like to read (*cough* Not C *cough*), but also because it is cleanly structured and includes Silverlight (that means Mac also!), WPF and XNA (Xbox360) versions. It illustrates a way one can factor their code into an engine and various hosts.

IMPORTANT NOTE: To run, Virtu needs two files that are not included: An image of the standard or preferably the enhanced Apple IIe monitor ROM needs to be copied as 'AppleIIe.rom' (16 KB) to the Roms directory. An image of the Disk II (16 sector) interface card ROM needs to be copied as 'DiskII.rom' (256 bytes) to the Roms directory. You'll also need some disk in the form of a ".nib" file like RasterBlaster.nib, for example. I can't give you those files.

After a successful build, you should be able to run the emulator and perform a self test by pressing the hallowed key combination Control+OpenApple+CloseApple+Reset.

Looking at the WpfKeyboardService.cs, I can see how those keys I don't have are mapped to keys I do:

ModifierKeys modifiers = keyboard.Modifiers;
IsOpenAppleKeyDown = keyboard.IsKeyDown(Key.LeftAlt);
IsCloseAppleKeyDown = keyboard.IsKeyDown(Key.RightAlt);
IsResetKeyDown = ((modifiers & ModifierKeys.Control) != 0) && keyboard.IsKeyDown(Key.F12);

IsCpuThrottleKeyDown = keyboard.IsKeyDown(Key.F8);
IsVideoFullScreenKeyDown = keyboard.IsKeyDown(Key.F11);
IsVideoMonochromeKeyDown = keyboard.IsKeyDown(Key.F9);

Looks like that's ALT, ALT, CTRL, F12 which gives me a weird series of self test screens then "System OK" which is a good sign.

image

This is nice, now I can do a little Applesoft BASIC by booting to the monitor with Ctrl-F12 then typing this, then RUN.

10 TEXT:HOME
20 ?"HELLO WORLD"

Thrilling!

image

It's really fun code to read and it's a lot cleaner than you'd think for an emulator, although there's the expected Giant Scary Switch Statements here and there. Other parts definitely feel like they've been brought along from the past, although, how else would you do them? (Don't look in VideoData.cs, your face will melt.) For example, here's how they draw text (remembering that we're not using Fonts here, we've got a REALLY low res screen):

private void DrawText40(int data, int x, int y)
{
int color = Machine.Settings.Video.IsMonochrome ? ColorMono00 : ColorWhite00;
int index = _charSet[data] * CharBitmapBytes;
int inverseMask = (_isTextInversed && !_memory.IsCharSetAlternate && (0x40 <= data) && (data <= 0x7F)) ? 0x7F : 0x00;
for (int i = 0; i < TextHeight; i++, y++)
{
data = CharBitmap[index + i] ^ inverseMask;
SetPixel(x + 0, y, color | (data & 0x01));
SetPixel(x + 1, y, color | (data & 0x01));
SetPixel(x + 2, y, color | (data & 0x02));
SetPixel(x + 3, y, color | (data & 0x02));
SetPixel(x + 4, y, color | (data & 0x04));
SetPixel(x + 5, y, color | (data & 0x04));
SetPixel(x + 6, y, color | (data & 0x08));
SetPixel(x + 7, y, color | (data & 0x08));
SetPixel(x + 8, y, color | (data & 0x10));
SetPixel(x + 9, y, color | (data & 0x10));
SetPixel(x + 10, y, color | (data & 0x20));
SetPixel(x + 11, y, color | (data & 0x20));
SetPixel(x + 12, y, color | (data & 0x40));
SetPixel(x + 13, y, color | (data & 0x40));
}
}

In Silverlight, they use the same (only) technique that Pete Brown's C64 emulator used to use, the new WriteableBitmap class. This means the XAML is just a single Image, and everything is a dynamically generated Bitmap. Here's the SilverlightVideoService.cs:

namespace Jellyfish.Virtu.Services
{
public sealed class SilverlightVideoService : VideoService
{
public SilverlightVideoService(Image image)
{
_image = image;
SetImageSize();

_bitmap = new WriteableBitmap(BitmapWidth, BitmapHeight, BitmapPixelFormat);
_pixels = new uint[BitmapWidth * BitmapHeight];

Application.Current.Host.Content.Resized += (sender, e) => SetImageSize();
}

[SuppressMessage("Microsoft.Usage", "CA2233:OperationsShouldNotOverflow", MessageId = "y*560")]
public override void SetPixel(int x, int y, uint color)
{
_pixels[y * BitmapWidth + x] = color;
_pixelsDirty = true;
}

public override void Update()
{
if (Application.Current.RunningOffline && /*_window.IsActive &&*/ (_isFullScreen != IsFullScreen))
{
_isFullScreen = IsFullScreen;
}

if (_pixelsDirty)
{
_pixelsDirty = false;
_bitmap.Lock();
for (int i = 0; i < BitmapWidth * BitmapHeight; i++)
{
_bitmap[i] = (int)_pixels[i];
}
_bitmap.Invalidate();
_bitmap.Unlock();
_image.Source = _bitmap; // shouldn't have to set source each frame; SL bug?
}
}

private void SetImageSize()
{
Content content = Application.Current.Host.Content;
int uniformScale = Math.Min((int)content.ActualWidth / BitmapWidth, (int)content.ActualHeight / BitmapHeight);
_image.Width = uniformScale * BitmapWidth;
_image.Height = uniformScale * BitmapHeight;
}

private const int BitmapWidth = 560;
private const int BitmapHeight = 384;
private static readonly PixelFormat BitmapPixelFormat = PixelFormats.Bgr32;

private Image _image;
private WriteableBitmap _bitmap;
private uint[] _pixels;
private bool _pixelsDirty;
private bool _isFullScreen;
}
}

It's a nice codebase and fun to step through. If you're interested in learning about emulation, check it out.

There are Wiki pages with details and quirks for each platform, WPF, XNA and Silverlight. There's still work to be done, so you might head over there and offer to help!

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 ORCS Web

Building your own Arcade Cabinet for Geeks - Part 7 - Success and Conclusion

June 6, '09 Comments [18] Posted in Arcade
Sponsored By

This is the seventh part of a multi-part series I'm going to do about assembling an Arcade Cabinet for my house. This series has two disclaimers:

Software Disclaimer 1: There's all sorts of legal issues around emulating arcade games. This series of posts has nothing to do with that. I do own some original arcade boards, but if you want to emulate arcade games with MAME (Multiple Arcade Machine Emulator), you can search the 'tubes. What I'm doing here is putting a computer in a pretty box.

Hardware Disclaimer 2: Many folks that build arcade cabinets have a purist view of how these things should be done. They will prefer original Arcade CRT monitors and more expensive, higher quality parts. I am more of a pragmatist. I also have no idea what I'm doing, so I've also got ignorance on my side.

Video Tour

Here's a short HiDef Video shot with my Creative Vado HD showing the Arcade Cabinet:

What I Learned

I learned:

  • You can cut corners, save money and still end up with a nice system, as long as you set your goals up front.
  • Steel is hard to cut. Know what you're doing, or get a pro.
  • Acrylic is hard to cut. Know what you're doing, or get a pro.
  • Painting and sanding something is the easiest way to make it look nice.
  • Making stuff with your hands is very satisfying and easier than you think (if you're a coder and you think this stuff is hard).
  • I could do it way better next time. ;)
  • I, myself, prefer to refurb old stuff rather than make new stuff.

All in all, the wife is amazed it looks so good, and she's said I can bring it into my den/office. She's nixed the living room...for now!

Total Cost: US$441

The Complete Series

  1. Cabinet and Power
  2. Monitor and Mounting
  3. Control Panel
  4. Sound and Lights
  5. Paint and Art
  6. Computer Hardware and Software
  7. Success and Conclusion

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 ORCS Web

Building your own Arcade Cabinet for Geeks - Part 6 - Computer Hardware and Software

June 6, '09 Comments [2] Posted in Arcade
Sponsored By

This is the sixth part of a multi-part series I'm going to do about assembling an Arcade Cabinet for my house. This series has two disclaimers:

Software Disclaimer 1: There's all sorts of legal issues around emulating arcade games. This series of posts has nothing to do with that. I do own some original arcade boards, but if you want to emulate arcade games with MAME (Multiple Arcade Machine Emulator), you can search the 'tubes. What I'm doing here is putting a computer in a pretty box.

Hardware Disclaimer 2: Many folks that build arcade cabinets have a purist view of how these things should be done. They will prefer original Arcade CRT monitors and more expensive, higher quality parts. I am more of a pragmatist. I also have no idea what I'm doing, so I've also got ignorance on my side.

Hardware

I, like most geeks, have a number of old computers lying around. I picked the best one, a Pentium 4 I used to use as a Media Center. It has a gig of RAM, runs Windows XP SP3 and has a nice fast ATI video card. I figured since I am just as interested in regular games as I am in Arcade Classics, it'd behoove me to have a decent machine in there.

IMG_0108 IMG_0300

The insides are HUGE as I yanked out the original arcade guts. There's lots of room and the back has two holes cut with screendoor material over them. I haven't had a heat problem yet, but I suspose I could easily add a small desk fan to blow air out the back. It would turn on with the rest of the equipment.

I setup a Microsoft Wireless Desktop 6000 mouse and keyboard (to avoid cords) and I may mount a sack or something on the back to hold them. I also plugged in a Wireless Xbox Controller and Xbox USB Gaming Receiver for games like Far Cry.

Software

I put lots of emulators for classics like C64, SNS, Ataris and other old hardware I have lying around. I set a few important BIOS settings that you shouldn't miss.

First:

  • Set your Power BIOS setting to "Always On" or "On After Power Loss."
  • Set your BIOS to "quick" or "silent" in order to speed up the boot.
  • Use "msconfig.exe" to set the time that Windows waits to boot up to some small number like 3 seconds.
  • Configure XP to automatically logon as some user of your choice, per KB3152321.

There's a number of popular "Front Ends" that you can put in your Startup Folder. Some folks replace Explorer.exe as the shell, but I think that's excessive. I just set my launcher to start immediately .

If you're looking for the prettiest, most "art focused" front end, then there is no other front-end than HyperSpin. It uses a file-based system of transparent PNGs and AVIs along with a compositing engine to make a truly beautiful circular menuing system.

Software is one of those things I'll spend the next year getting just the way I want it.

Next Up: Success and Conclusion

  1. Cabinet and Power
  2. Monitor and Mounting
  3. Control Panel
  4. Sound and Lights
  5. Paint and Art
  6. Computer Hardware and Software
  7. Success and Conclusion

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 ORCS Web

Building your own Arcade Cabinet for Geeks - Part 5 - Paint and Art

June 6, '09 Comments [1] Posted in Arcade
Sponsored By

This is the fifth part of a multi-part series I'm going to do about assembling an Arcade Cabinet for my house. This series has two disclaimers:

Software Disclaimer 1: There's all sorts of legal issues around emulating arcade games. This series of posts has nothing to do with that. I do own some original arcade boards, but if you want to emulate arcade games with MAME (Multiple Arcade Machine Emulator), you can search the 'tubes. What I'm doing here is putting a computer in a pretty box.

Hardware Disclaimer 2: Many folks that build arcade cabinets have a purist view of how these things should be done. They will prefer original Arcade CRT monitors and more expensive, higher quality parts. I am more of a pragmatist. I also have no idea what I'm doing, so I've also got ignorance on my side.

Paint is pretty easy. Two coats people say. I am a believer. ALWAYS two coats. For this project I also did a few other things. John suggested that we use the orbital sander to really give the wood a little texture so the paint would grip. The sander also took off the side art nicely.

Paint

I sanded the heck out of it. I used some wood putty to fill in some holes and nasty parts, let them dry, then sanded over them. Then I used a small foam roller (brushes are too slow, and I really don't like the texture they often leave) to do a first coat with a "Killz" tinted primer. This stuff is amazing. It really covered stains and stickers and all sorts of crap.

IMG_0036 IMG_0332

Then I taped off all the important bits with blue painters tape. The Killz Tinted Primer is nice because I only needed an hour or so (I waited 2) to put the first coat of paint.

Cost: US$35
Cost So Far: US$341

I waited overnight for the second coat of black and let me tell you, it really took the paint to the next level. It looks cleaner, smoother, sharper, and the coverage is perfect. Really, people. Two coats. It's like flossing. If you don't floss, don't even bother brushing. Two coats, or don't bother. I'm a believer.

Art and Marquees

I went over to Mame Marquees to get the side art. I've heard nothing but nice things about their work. They often have sales and overstock, and while I was originally planning on custom "Hanselcade" art, I fell line love with this classic "atomic blue" design.

DSC_0139DSC_0141

Cost: US$100
Cost So Far: US$441

The side art came with a sticky back and was surprisingly easy to install without any bubbles. You just start at the top, remove an inch of the back, and slowly remove the back pulling down as you smooth from the top. I would say it was skill, but the decals are very think vinyl and of high quality. I don't know what kind of printer he has but it's worth the money. The art really gave the project "pop!" IMHO.

DSC_0135   DSC_0136

As I mention in the Control Panel section, the vinyl for the control panel was sandwiched between the steel and the acrylic. No adhesive was used as the 19 buttons and the pressure of the outer screws hold it all in place.

IMG_0172IMG_0179

Now, I get to put the computer inside and hook it all up!

Next Up: Computer Hardware and Software

  1. Cabinet and Power
  2. Monitor and Mounting
  3. Control Panel
  4. Sound and Lights
  5. Paint and Art
  6. Computer Hardware and Software
  7. Success and Conclusion

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 ORCS Web

Building your own Arcade Cabinet for Geeks - Part 4 - Sound and Lights

June 6, '09 Comments [0] Posted in Arcade
Sponsored By

This is the fourth part of a multi-part series I'm going to do about assembling an Arcade Cabinet for my house. This series has two disclaimers:

Software Disclaimer 1: There's all sorts of legal issues around emulating arcade games. This series of posts has nothing to do with that. I do own some original arcade boards, but if you want to emulate arcade games with MAME (Multiple Arcade Machine Emulator), you can search the 'tubes. What I'm doing here is putting a computer in a pretty box.

Hardware Disclaimer 2: Many folks that build arcade cabinets have a purist view of how these things should be done. They will prefer original Arcade CRT monitors and more expensive, higher quality parts. I am more of a pragmatist. I also have no idea what I'm doing, so I've also got ignorance on my side.

The sounds and lights part was the easiest part of the whole thing so far. After the challenging control panel, this was a nice change of pace.

Sounds

This was pretty straight-forward. There's a compartment at the top of the cabinet. I took some zip-ties and essentially lashed some computer speakers, facing down, to the sides of the inside of this area. There's speaker-shaped drilled holes in the bottom of the top, so the sound can get out. There's a subwoofer in very bottom of the machine, next to the computer.

IMG_0086 IMG_0108

The speakers and sub were just old computer audio stuff I had lying around. I could have reused the tinny speakers that were already in it, but I wanted to punch up the sound a smidge. You can see the subwoofer in the right-hand side picture, sitting on a sturdy shelf, next to the coin collection box.

Cost: US$0 (Had speakers lying around.)
Cost So Far: US$306

Now, lights.

Lights - Marquee

In the top compartment, we put in a small 18" florescent light tube. Then when we ordered the art from Scott at Mame Marquees, we made sure it was translucent and that he'd throw in the Plexiglas for free. I didn't want to cut any more of that again. :)

IMG_0073IMG_0299

Then I threaded the AC cord and plug down the inside-back of the cabinet and plugged it into the smart Power Strip. This was clean, easy and had a big pay off. It looks bright in the picture because of the camera, but wait until the end. It looks niiiice.

Lights - Coin Door

In every write-up of every Arcade Cabinet I've ever read, folks have fun doing the coin door lights. You can't have one of these without doing it. It's just required. It also requires some thought, which I forgot.

I started by going to Radio Shack and buying two random, but bright-looking LEDS. Then I tool an old USB cable, which I knew was 5 volts and I hooked it up directly to the LED. It was bright for about 5 minutes then died. Then I remembered I was an idiot. I was so excited I just went nuts and forgot about resistors.

I went to an LED Calculator and put in the source voltage, 5V, the LED voltage, 3.5V and their current, 20mA. There would be two LEDs, so it spit out this recommendation for two 82 ohm resisters in parallel.

image

I got my multi-meter out and found an 85 ohm resister I had lying around. I figured that was close enough. I busted out the soldering iron and got to work.

IMG_0346 IMG_0347 IMG_0348

I plugged my old USB cable into an old cell-phone BlackBerry AC adapter that put 5V and .5A through the USB cable and there was light!

Cost: US$5 (Resisters, Solder, Random Radio Shackiness.)
Cost So Far: US$311

Ok, time to paint.

Next Up: Paint and Art

  1. Cabinet and Power
  2. Monitor and Mounting
  3. Control Panel
  4. Sound and Lights
  5. Paint and Art
  6. Computer Hardware and Software
  7. Success and Conclusion

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 ORCS Web
Page 1 of 2 in the Arcade category Next Page

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