Rescuing the Tiny OS in C#
About four years ago (in the middle of the last fifteen years of writing software for money) I graduated from college. I took a round-about way through school at OIT - my four-year degree took eleven years (I actually had to make deals with four different deans to allow me to keep my credits from expiring) but it turned out OK.
Some where in the middle of going to school at night we were asked in an Operating Systems class to write a "Virtual CPU and OS." More of an interpreter, we were given a description of the OS, some op-codes and an ASM-like machine language. We were to write the OS, and feed test programs into the system. It was a fun exercise and a good way to teach folks how virtual memory and memory mapping works, threads vs. processes, etc. I was the only student who chose to write it in managed code. Quite the coup for 2002.
I turned in the project and threw it up on GotDotNet (this link will be dead in July of 2007), but of course, GDN will be gone soon. Just so it wouldn't be lost, it's now available here in C#. It's actually kind of fun, I think, as I put in some "jokes" - funny to me at least. For example, the OS pages it's virtual memory to XML files when it runs out of "physical" memory. It behaves like a real little OS. It'll thrash with low memory, and get fragmented if a lot is going on.
A tiny virtual CPU and OS written entirely in C#. The TinyOS simulates the scheduling, memory management (including paging and virtual memory) and other operations of theoretical Operating System. You’ll see many Framework classes and techniques in use including Regular Expressions, XML Serialization, generated Strongly Typed Collections, XML Comments and a generated Help file. You won’t get much useful work out of the TinyOS itself, but the techniques you’ll learn can be applied to real life.
Since this little ditty, others who really know what they are doing have written more serious OS's in C# that are worth looking at.
I presented on my little CPU/OS at TechEd Malaysia 2002, and the PPT slides are available as well. I also did it again in VB.NET if you like with a little trouble.
I looked at this code in 2005 and was shocked and offended with myself - well, not really, but it was enlightening to say the least.
Download Tiny OS in C#
Download Tiny OS in VB.NET
Download PPT Slides on the Tiny OS presented at TechEd Malaysia 2002
Go find some code that you wrote at LEAST FIVE years ago and tell me how it looks 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.
About Newsletter
When no parameters are entered, it tells you:
"Usage: OS membytes [files]"
So you run it like this:
OS.exe 1024 scott1.txt
for example -- meaning, allocating 1024 bytes, and execute the file "scott1.txt" (which you'll find provided with it).
Okay -- when you run these files, which are short machine-code programs, a lot of stuff scrolls up the screen... you need fast eyes ;-)
I found what the instructions mean:
public enum InstructionType {
Noop = 0,
Incr,
Addi,
Addr,
Pushr,
Pushi,
Movi,
Movr,
Movmr,
Movrm,
Movmm,
Printr,
Printm,
Jmp (Good old Jmp!),
Cmpi,
Cmpr,
Jlt,
Jgt,
Je,
Call,
Callm,
Ret,
Alloc,
AcquireLock,
ReleaseLock,
Sleep,
SetPriority,
Exit,
FreeMemory,
MapSharedMem,
SignalEvent,
WaitEvent,
Input,
MemoryClear,
TerminateProcess,
Popr,
Popm
}
So first up i've written up a suite of Nunit tests to test if these instructions each display correct behaviour. (Mostly passed -- one suprise mistake in there!)
Then I wrote up a short assembly language compiler (in powershell) that lets you assign and use aliases for these commands, and compile aliased code into machine at compile time.
Next I wrapped some obvious functions (some 2d analysis, bitblt, and the like) into a small library that gets preloaded into the virtual os, to see if we can create a higher level abstraction as a basis for future
And then as a demonstration, i've written a gif to ascii converter based on this OS, and tried to out converting a picture of anakin skywalker into a 64*64 ascii picture
Check out all this and more here:
http://secretGeek.net/sos_test.aspx
I have to take the bait on your comment about telling you how our five year old code looks. Five years ago, I would have been at Commerce Bank for a little over a year, and hoo-boy, did I write some embarrassingly bad classic ASP and VB in that era. Those first couple years at Commerce were a crash course for me in a lot of things - web technology, pseudo-OOP (realizing that we're talking about VB 6), COM, enterprise development, etc. Five years later, I can say with confidence that this code is amazingly bad; Especially anything where I tried to use XML for no reason other than because it's what the cool kids were doing at the time. Any of the code I wrote in that timeframe should be treated as hazardous material and properly disposed of. Thankfully, the programmers who maintain that application today have long since replaced most of my offending code - except the nifty little VB app I wrote to bridge our contact center softphones to our ASP-based web app via DDE.
Prior to coming to Commerce, my start was as a subcontractor for an independent consultant who had a thing for XBase languages. When I started as his apprentice at the age of 12, it was Clipper. Then FoxPro 2.6 for Windows. Then Visual FoxPro 3.0, 5.0, and, by the time I graduated from college and went to work for him full time, Visual FoxPro 6.0. If I were to look around my home office today, I could probably find some code left over from that bygone era, but that begs the question: Why on Earth would I want to? :)
(Disclaimer: These comments are solely my opinion and do not represent or express the position of Commerce Bancshares, Inc.)
http://bloggingabout.net/blogs/adelkhalil/archive/2007/04/02/what-is-you-5-years-old-code-look-like.aspx
Anyhow, I remember doing this project in class as well. I did mine in VB6. And it was a pain.
I've been stalking your blog for some time now and it's a great lunch break activity! One quick question... what tool did you use to compile the generated help file? Do you recommend it?
It's time to grab what you might need from GDN. I don't think their stuff will be relocated once the site is gone.
Even looking back, the code I wrote for that isn't too bad, although there are a few minor things I would have done differently. Most of my other old code is horrible, however. My first real programming experience came on the TI-85 graphing calculator (my friend wrote a nifty little RPG called CalcRPG back then, it was pretty cool in 7th grade). Eventually, we attempted to port CalcRPG (now graphical on the TI-92) into VB and created VBRPG which I think has some of the worst code in the history of the world. It's gotten much better over the years and the stuff I did in the last two years really isn't all that bad.
It's amazing how quickly your techniques and problem solving can change or go a way depending on their use. I would like think playing with my electronic Sudoku (day and night) has helped with it. When I started with that, I was taking about 10-15 minutes on an easy level puzzle. Now that's down to less than 3 on average. The more difficult ones would take anywhere from 1-2 hours. I'm now down to 15-20 minutes.
One other thing that I noticed was the amount of comments I left myself (and others). There are MANY more now as I am figuring out that they are truly needed. One of them I know I wrote, but I just don't remember it.
I'm going to play with TinyOS now and try the 2002 - 2005 conversion. Then learn a bit more about OSes. Thanks for your work and I'm glad you were able to actually use something from college in the real world! ;-)
Love then new design for your blog by the way!
--jb
That might take me a little while to figure out since I am pretty much clueless on that concept right now. But your work here jsut might give me the starting point to look at that.
Thanks again!
Comments are closed.