Click here to Skip to main content
15,894,343 members
Articles / General Programming / Algorithms

A Simulator for Knuth's MIX Computer

Rate me:
Please Sign up or sign in to vote.
4.91/5 (26 votes)
2 Feb 2011CPOL7 min read 55.9K   668   34  
Assembler and Simulator for Don Knuth's MIX Computer from The Art of Computer Programming.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MIXLib;
using MIXLib.Util;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace MIX
{
    class Program
    {
        private static string GetInputFile(Dictionary<string, string> cmdLine)
        {
            var a = cmdLine.Keys.Where(cl => !cl.StartsWith("-") && !cl.StartsWith("/"));
            if (a.Count() > 0)
                return a.First();

            return null;
        }

        static void Main(string[] args)
        {
            Dictionary<string, string> aliases = new Dictionary<string, string>();
            aliases.Add("-d", "--deck");
            aliases.Add("-b", "--binary");
            aliases.Add("-?", "--help");
            aliases.Add("-h", "--help");

            Dictionary<string, string> cmdLine = CommandLineHelper.SplitCommandLine(Environment.CommandLine, aliases, true);
            if (cmdLine.Count == 0)
            {
                Console.WriteLine("This is MIX v0.1, (c) 2009 George Tryfonas\nAn mplementation of the machine described by Don Knuth.\n\nType '?' or 'help' at the prompt for instructions.\n");
                MIXController c = new MIXController();
                c.Interface();
            }
            else
            {
                Stream stream;

                MIXMachine machine = new MIXMachine();
                string inFile = GetInputFile(cmdLine);
                if (string.IsNullOrEmpty(inFile))
                    stream = Console.OpenStandardInput();
                else
                    stream = new FileStream(inFile, FileMode.Open);

                if (cmdLine.ContainsKey("--deck"))
                {
                    machine.RedirectDevice(MIXMachine.CARD_READER, stream);
                    machine.LoadDeck();
                }
                else if (cmdLine.ContainsKey("--binary"))
                {
                    IFormatter formatter = new BinaryFormatter();
                    MIXWord startLoc = (MIXWord)formatter.Deserialize(stream);
                    List<MemoryCell> data = (List<MemoryCell>)formatter.Deserialize(stream);

                    machine.LoadImage(data);
                    machine.PC = startLoc;
                    machine.Run();
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Greece Greece
I am a software developer (mainly in C# and T-SQL) for a project management company in Athens, Greece. I have been working with computers since early 1987. I am adept at Pascal, C, C++, Java (my MSc was sponsored by Sun Microsystems), Lisp, Scheme, F#, C# VB.Net, Perl and some others that are too obscure to mention. When I want a quick and dirty solution to a programming problem I use a functional language, such as Haskell, Scheme or, more recently, F#.

I also play the keyboards and compose music.

---------------------------------------------------------

MSc Distributed Systems and Networks - University of Kent at Canterbury
BEng Computer Systems Engineering - University of Kent at Canterbury

Comments and Discussions