• Hello MLAers! We've re-enabled auto-approval for accounts. If you are still waiting on account approval, please check this thread for more information.

Possibly writing an emulator... of sorts

dav7

6502
[ To achieve a more widespread response, this post has been duplicated at archlinux.org (my distribution's forum). ]

As a sort of toy to play with in recognition of old technology, I want to try my hand at writing a kind of emulator similar to an old Macintosh, except using simpler principles.

I want a fixed amount of video memory for a 320x200 or 512x384 (not sure which) 1-bit (b&w) display, and basic keyboard and mouse support. Nothing else.

From there, I want simple routines to be able to detect where the mouse cursor is and/or if the keyboard is being used, and from there flick bits on and off in the video array to form pictures.

I want this emulator to run between 500kHz and about 5MHz - that should get everyone doing some serious optimization :p although I would of course let the emulator run at full host speed for testing/developmental purposes.

If all works well, I hope to build up enough awesomeness to get a basic GUI running, although nothing serious.

There's just a small issue: I'm not too sure how to do this. Of course, for this to be remotely authentic, I'd make a real CPU and an assembly language for it, but that's a bit (very large bit) over my head (although if I did that I could load the language onto an FPGA... :p ) so I was considering somehow using C and abusing dynamic libraries to get myself a "binary" file format.

The other issues are the CPU - how do I execute code at a set speed? - and mouse/keyboard event reporting. I have no idea how to do that, except for using interrupts, which is obviously out. And the screen is another issue - how do I refresh my "video memory"? At the end of each "CPU iteration" do I just run through the video memory area armed with (since I plan to use Xlib) XSetForeground() and XDrawPoint()?

So... input? suggestions? Thanks. :D

-dav7

 
As a sort of toy to play with in recognition of old technology, I want to try my hand at writing a kind of emulator similar to an old Macintosh, except using simpler principles.
If you want to emulate something, what is it you are actually emulating?

Are you are emulating some fictitious computer? If so, does that actually count as emulating?

I want this emulator to run between 500kHz and about 5MHz
What does that mean? What is it that you are measuring the frequency of?

 
returningmacuser, Dennis Nedry: MicroMac and µMac sound nice... but I don't actually want to emulate a Mac.

porter: What I want to "emulate" isn't a real PC, so I don't really know if it could be called emulating. It is indeed a fictitious computer.

And my speed reference refers to how fast I want the code this simulator/emulator/whatever-it-is will run to run.

-dav7

 
Last edited by a moderator:
And my speed reference refers to how fast I want the code this simulator/emulator/whatever-it-is will run.
But it's a meaningless number the way you have quoted it. For instance a 1Mhz 6502 and 4Mhz Z80 were equivalent because the 6502 took one clock cycle for the fetch execute cycle, and the Z80 took four.

So what is the frequency measuring?

I've used plenty of emulators, 8080, Z80, 6502, 680x0, even PDP-11, what is this fictiuous one doing that the others don't? Or is it because you want to write one?

 
returningmacuser, Dennis Nedry: MicroMac and µMac sound nice... but I don't actually want to emulate a Mac.
porter: What I want to "emulate" isn't a real PC, so I don't really know if it could be called emulating. It is indeed a fictitious computer.

And my speed reference refers to how fast I want the code this simulator/emulator/whatever-it-is will run to run.

-dav7
I knew that from the first post. Is the fictional computer you're planning to emulate going to have a 68000 or something else?
 
From there, I want simple routines to be able to detect where the mouse cursor is and/or if the keyboard is being used, and from there flick bits on and off in the video array to form pictures.
I would do this as follows:

1. thread/process one, real user interface that maps X api calls to memory mapped IO in block of memory.

2. thread/process two, a CPU emulator that just continually runs the fetch/execute cycle using the same block of memory as it's address space.

3. Signals delivered to the second process I mentioned count as interrupts to the processor, eg the signal handler causes the emulated CPU to emulate an interrupt

4. the UI thread/process responds to notifications from the CPU thread/process.

5. In the CPU thread/process everytime a memory access occurs on a guarded piece of memory notify the UI process, this can be done by doing an AND/XOR on every address to which a write is done, if the result is zero then signal the UI process.

This could either be done as a single process with two threads, or a pair of processes with shared memory to act as the memory space of the emulated CPU.

 
Ah. I see what you mean.

Well, since this is merely because I want to write one, I'd probably want the frequency to represent how often I want the "execute" loop to run, so, at 1MHz, the execute loop would run 1 million times per second.

That doesn't mean the whole program will be run 1 million times per second - I just want single instructions to be executed at each "cycle".

Although now I realize that it's kinda hard to slow code execution down to an instructional/cyclic level without designing the whole CPU myself, plus take everything else you've mentioned into account, so I'm not really sure if I want to do this anymore :/

I guess the two things I'd probably want to do now would be:

- See how Toolbox ROM and bootloader procedures on the Macintosh work and possibly make something that boots up using these routines, or

- Actually think about designing some kind of language (probably not an assembly language, because I'd have to implement quite a few instructions to make my "emulator" of sorts useful)

I'm thinking option #1 would be generally more interesting, both to myself and to others, since real macs would be able to run it :p but would it be legal?

-dav7

 
Last edited by a moderator:
- See how Toolbox ROM and bootloader procedures on the Macintosh work and possibly make something that boots up using these routines
You'd be only the second person to do it, the first being the writer of the original startup code.

NetBSD, Linux-68k, A/UX all use the Mac OS to do the bootstrap. It's a nightmare unless you restrict yourself to a single class of machine.

The Class Macintosh OS is not a good example of a clean operating system design. It's just full of kludges, bodges, shoe-horns, dead-ends and twisty-little-passages-all-alike.

 
pseudo-code machine

a p-code machine or pseudo-code machine is a specification of a CPU whose instructions are expected to be executed in software rather than in hardware /
Programs [in] p-code are interpreted by a software program that emulates the behavior of the CPU
Once I understood your idea, it sounded a bit like you want to create something like the Java virtual machine. Generically, these are called p-code, or pseudo-code machines. And then to create an OS and GUI on top of that.

What you are talking about is quite a large undertaking. If you are doing this to teach yourself low-level operating system design, fair enough. Sounds like a plan.

But unless you also want to learn all about designing and building virtual machines, and create all their opcodes and a programming language, -and- get all that working before you even start on the OS and GUI, maybe you wanna pick an existing virtual machine spec - preferably one that you can just apt-get and install. Maybe even Java itself - who knows, you could eventually have the thing run in a browser.

Failing that, one that you implement yourself, but to an existing spec, with an existing support/enthusiast/fanbase. There should be a few available, and hopefully the article above is a good place to start. There's probably a swathe of Usenet groups under the comp. subheading, once you know what terms to look for.

I'm sure there is at least one specification that is designed for teaching OS design and comes with a famous textbook on the subject, /edit/ that would be Minix I'm thinking of - see below.

Then you can move on to the "easy" part - an OS and GUI [:o)] ]'>

Treat the idea of loading and running the MacOS Toolbox ROM as a seperate (but related) project. There you will have to emulate either a 68k CPU or a Pascal virtual machine, plus a whole bunch of other stuff I don't understand at all. But I guess essentially recreating the effort of other Mac emulators.

If you want to do that as well, cool - consider (also/instead/first) taking an existing open-source emu and picking it apart.

See also:

http://en.wikibooks.org/wiki/Category:Operating_System_Design

http://en.wikipedia.org/wiki/JNode

http://en.wikipedia.org/wiki/Minix

 
Last edited by a moderator:
Actually, implementing it in Pascal might be a good call -

http://en.wikipedia.org/wiki/Pascal_(programming_language)

Pascal was the primary high-level language used for development in the Apple Lisa, and in the early years of the Mac; parts of the original Macintosh operating system were hand-translated into Motorola 68000 assembly language from the Pascal sources. /

In order to rapidly propagate the language, a compiler "porting kit" was created / that included a compiler that generated code for a "virtual" stack machine / the Pascal-P system. / at least one system, the notable UCSD implementation, utilized it to create the interpretive UCSD p-System.
See also:

http://en.wikipedia.org/wiki/Object_Pascal

All of the above is open to correction by others who know what they're talking about (ie, not me) and your own research.

 
Failing that, one that you implement yourself, but to an existing spec, with an existing support/enthusiast/fanbase.
If you want to become a hero, get involved with the PlayStation 2 emulator folks. MIPS is a well defined architecture with a few other bits involved but is certain classed as interesting and challenging.

 
Wow O.o

This sounds interesting and all but I don't find "systems programming" very interesting - I'm more of a GUI designer. Occasionally I get sparks of interest in doing something non-graphical - like this - but... eep.

This would probably work out to be way too boring (with sporadic periods of fascination that wouldn't last long) to hold my attention long enough to finish it, to be honest :p

-dav7

 
The Class Macintosh OS is not a good example of a clean operating system design. It's just full of kludges, bodges, shoe-horns, dead-ends and twisty-little-passages-all-alike.
To tell you the truth, I find 68k Macintosh computers in general to be somewhat inefficient compared to many other 68k computers, most notably the Amiga and Atari ST.
 
Question: is your main ambition educational? ie: to learn about the design of operating systems and/or emulators.

Or do you really hope to develop a working Mac compatibility environment?

 
I think what I want to make is more of a simulator: something that achieves two goals, to run at a set (slow) speed so that graphics redraw slowly (one target), and that displays at 1bpp (the other target).

I'm not too sure how to achieve something like this, but I think it'd probably be fun, and a great UI prototyping opportunity (which is actually a part of my motivation to make this).

-dav7

 
Okay. Please excuse my earlier reading comprehension failitude. Now that I've had the requisite amount of intravenous caffeine, I think I get what you're about. I'll try and express it in my own words - tell me if I'm getting warm.

What you're proposing is essentially the same as building a scratch-built, homebrew computer, like those linked off the 6502 thread, and a homebrew OS to run on it. Except that you want to do it in software, rather than hardware.

 
What you're proposing is essentially the same as building a scratch-built, homebrew computer, like those linked off the 6502 thread, and a homebrew OS to run on it. Except that you want to do it in software, rather than hardware.
except

This sounds interesting and all but I don't find "systems programming" very interesting
If you build it yourself to your own design you need to do the systems programming. When a CPU starts, even an emulated CPU, it has to run some code, because that's what CPUs do. Where is that code going to come from.

I'm more of a GUI designer
There is a huge difference between a GUI designer and a graphics programmer. Using QuickDraw to get coloured pixels on the screen is one level, bit twiddling in memory is another, I will leave you to guess which is simpler and more productive for the programmer.

 
Back
Top