Chaoji Li
and 1 contributors


Hardware::Simulator::MIX - A simulator of Knuth's famous virtual machine


    use Hardware::Simulator::MIX;

    my $mix = new Hardware::Simulator::MIX;
    while (!$mix->is_halted()) {


This implementation includes the GO button and the default loader is the answer to Exercise 1.3 #26.

Trace execution count and time for every instruction.

For detailed architecture information, search MIX in wikipedia.


    $mix = Hardware::Simulator::MIX->new(%options);

This method constructs a new Hardware::Simulator::MIX object and returns it. Key/value pair arguments may be provided to set up the initial state. The following options correspond to attribute methods described below:

    KEY                     DEFAULT
    -----------             --------------------
    max_byte                64
    timeunit                5   (microseconds, not milliseconds)

max_byte is the maximum value a MIX byte can express. timeunit is one memory access time. According to knuth, in 1960s, one time unit on a high-priced machine is 1 us. and for a low cost computer it is 10 us. To change the default settings, do like this:

    $mix = Hardware::Simulator::MIX->new(max_byte => 100, timeunit => 10);



Accessing registers:


It is a reference to a MIX word. A MIX word is an array of six elements. Element 0 is the sign of word, with value either '+' or '-'. Elements 1~5 are MIX bytes with numerical values. Available registers are listed below:

    REGNAME                FORMAT
    -----------            -----------------------
    rA                     [?, ?, ?, ?, ?, ?]
    rX                     [?, ?, ?, ?, ?, ?]
    rI1                    [?, 0, 0, 0, ?, ?]
    rI1                    [?, 0, 0, 0, ?, ?]
    rI2                    [?, 0, 0, 0, ?, ?]
    rI3                    [?, 0, 0, 0, ?, ?]
    rI4                    [?, 0, 0, 0, ?, ?]
    rI5                    [?, 0, 0, 0, ?, ?]
    rI6                    [?, 0, 0, 0, ?, ?]
    rJ                     [?, 0, 0, 0, ?, ?]
    pc                     Integer in 0..3999

Note: the names are case sensitive.

    $mix->set_reg($reg_name, $wref);

For registers rI1 ~ rI6, rJ, the bytes 1~3 are always set to zero. You should not modify them. Or the result will be undefined.


A MIX memory word is similar to the register rA and rX word.

    $mix->read_mem($loc, $l);
    $mix->read_mem($loc, $l, $r);

Return a MIX word from memory. $loc must be among 0 to 3999. If field spec $l and $r are missing, they are 0 and 5; If $r is missing, it is same as $l.


equals to


Write memory.

    $mix->write_mem($loc, $wref, $l, $r);

$mix->get_cmp_flag() returns an integer. If the returned value is negative, the flag is "L"; if the return value is positive, the flag is "G"; if the return value is 0, the flag is "E".

$mix->get_overflow() return 0 if there is no overflow. return 1 if overflow happen.

$mix->get_current_time() returns the current mix running time in time units since the last reset.

$mix->is_halted() returns 1 if the machine halts.


Get the execution count of an instruction
Get the time spent on an instruction

The result is in MIX time units.




Reset machine status to initial state. Automatically invoked when a MIX object is created. Clear the machine memory and registers to zero. Discard all statistics. PC will be cleared to zero, so next time the machine will read instruction from location zero.


Execute one instruction. Return 1 if success, otherwise return 0. Please check the machine status when this method returns 0.


Simulate MIX go button. Load a card into location 0, and then execute instructions from 0 until a halt condition is met, either by error or by the instruction HLT.


General information about MIX io devices.

    Unit number          Device            
    ===========          ======
         t               Tape (0 <= t <= 7)
         d               Disk or drum (8 <= d <= 15)
         16              Card reader
         17              Card punch
         18              Printer
         19              Typewriter and paper tape

Unit 16,17,18 are oftener used.

Each type of device has its own buffer convention. Card reader 's buffer is an array of strings. Each string is a card. The maximum length of a card is 80 characters. The first item in the array is always the first to be consumed by the machine. When MIX executes one IN ?,?(16) instruction, the first card will be read in, and that card is also discarded out of the buffer.

Printer's buffer is for output, it is also array of strings. Each string is a page.

$mix->add_device($devnum, $buf)

Register a device with its buffer.


Return the reference to the device buffer.


Load the first card in the card reader buffer into memory, starting from location $loc.


When you press the MIX GO button, MIX firstly loads the first card into memory 0000~0015, and then executes from location 0.

Please refer to Section 1.3.1, Exercise 26.


Chaoji Li<>

Please feel free to send a email to me if you have any question.


The package also includes a which assembles MIXAL programs. Usage:

    perl <yourprogram>

This command will generate a .crd file which is a card deck to feed into the Typical usage:

    perl <yourprogram.crd>