A Project With Source Code: An Updated Graphical, Hexadecimal LMC Simulator

Author’s Note: There were several details of my original simulator which I was dissatisfied with, even if the whole package did manage to properly simulate the hexadecimal variant of the LMC source code that it was meant to. The vector graphics were primitive, the toggle switches were far too responsive to extended key presses for my liking and the peculiar 640×250 resolution prevented the program from being easily made fullscreen. What’s more, each of the buttons and LEDs were placed based on “magic numbers” which were hardcoded into the source. None of these problems proved insurmountable, and I have now developed a slightly more sophisticated version of the simulator with bitmap graphics and switches which have a delay timer built into them.

The project still isn’t perfect; the appearance of the graphics is still amateur, although more appealing than the original version, and it doesn’t exactly mimic the operation of a comparable real-world machine, like the PDP-8 – to do that, I’d need to change the operation towards having a “load address” switch rather than having the switch positions change every time a new address is loaded. This wouldn’t be an insurmountable prospect either, although I prefer the switches being in sync with the LEDs for aesthetic reasons. A real-world LMC computer doesn’t exist, so I don’t have to worry about complete historical accuracy.

The controls are as they were in my original simulator, which is still available under the GNU General Public Licence as before. You will also require a number of graphics for the front plate, the LEDs and the switches. You can either use the graphics here, which you’ll have to manually convert to PCX format, or create your own, as long as they conform to the 640x250px size of the front plate, the 20x20px size of the LEDs and the 20x35px size of the toggle switches.

Note: The layout of this blog format doesn’t extend to the 80 columns required to show all of the source code appropriately. You can circumvent this restriction by dragging the mouse over all of the source code and copying it to a text editor; the hidden code will show up.

/* lmc_gui: A graphical simulator for the Little Man educational instruction
   set, modified for hexadecimal operation.
   Copyright (C) 2012  Richard Kiernan

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <allegro.h>
#include <stdio.h>
#include <time.h>

#define MAILBOXES 0x100 /* Hexadecimal LMC instruction set has 255 */
			/* "mailboxes" */
#define INSTRUCTION_MAX 0xFFF /* Highest value of LMC instructions/data */
#define OP_SPLIT 0xFF /* Denotes the split between opcodes and operands */
#define INSTRUCTION_REST 15 /* The pause time between executed instructions */
			    /* in milliseconds */

#define LED_WIDTH 20 /* LEDs are 20 pixels wide, 20 pixels high */
#define LED_HEIGHT 20
#define SWITCH_WIDTH 20 /* Switches are 20 pixels wide, 35 pixels high */
#define SWITCH_HEIGHT 35

#define BUFFER_HEIGHT 115 /* The black "buffer" on the top and bottom of the
			   screen is 115 pixels high. */

/* We want milliseconds, not full seconds! */
#define CLOCKS_PER_MS (CLOCKS_PER_SEC / 1000)

/* LED structure */
struct individual_led {
    int light_x, light_y;
    int activity_status;
};

/* LED/Switch combination structure */
struct led_switch {
    int light_x, light_y;
    int switch_x, switch_y;
    int activity_status;
    clock_t last_access_time;
};

/* Function prototypes */
void setup_screen(void);
void setup_structs(void);
void setup_components(void);
void setup_front_plate(void);
void setup_accumulator(void);
void setup_memory_addresses(void);
void setup_memory_contents(void);
void setup_power_switch(void);
void setup_clear_switch(void);
void setup_execute_switch(void);
void setup_input_light(void);
void setup_uinput_switch(void);
void initialise_memory(void);
void get_input(void);
void toggle_power(void);
void toggle_address_switch(int toggle);
void set_contents(void);
void toggle_contents_switch(int toggle, char mode);
void toggle_clear_switch(void);
void toggle_execute_switch(void);
void execute_instructions(void);
void set_accumulator(void);
void get_program_input(void);

/* Program variables */
int address_counter = 0; /* Address counter */
int accumulator = 0; /* Accumulator contents */
int input_temp = 0; /* Temporary storage for user input */
int mailbox[MAILBOXES]; /* "Mailboxes" - memory storage slots */

/* Program structures */
struct individual_led accumulator_led[12];
struct individual_led awaiting_input;
struct led_switch address_switch[8];
struct led_switch contents_switch[12];
struct led_switch power_switch;
struct led_switch clear_memory;
struct led_switch execute_switch;
struct led_switch user_input;

/* Program bitmaps */
BITMAP *front_plate = NULL;
BITMAP *led_on = NULL;
BITMAP *led_off = NULL;
BITMAP *switch_on = NULL;
BITMAP *switch_off = NULL;

int main(void)
{
    /* Initialise program */
    allegro_init();
    setup_structs();
    setup_screen();
    install_keyboard();
    initialise_memory();

    while (!key[KEY_ESC]) {
	if (keypressed()) {
	    get_input();
	}
    }

    /* Clean up */
    allegro_exit();
    return 0;
}

/* setup_structs: Assigns values to all of the appropriate structures in the
   program */
void setup_structs(void)
{
    int i;

    /* Start with the accumulator LEDs and contents LED/switch combos -
       12 of each */
    for (i = 0; i < 12; i++) {
	/* The accumulator LEDs start 50 pixels in, and are spaced at 30 pixel
	   increments. */
	accumulator_led[i].light_x = 50 + (i * 30);
	/* The accumulator LEDs are placed 40 pixels below the top edge of the
	   computer box. */
	accumulator_led[i].light_y = BUFFER_HEIGHT + 40;
	/* All of the LEDs are turned off by default. */
	accumulator_led[i].activity_status = 0;

	/* The contents LEDs and switches start 50 pixels in, and are spaced
	   at 30 pixel increments. */
	contents_switch[i].light_x = contents_switch[i].switch_x =
	    50 + (i * 30);
	/* The contents LEDs are placed 160 pixels below the top edge of the
	   computer box. */
	contents_switch[i].light_y = BUFFER_HEIGHT + 160;
	/* The contents switches are placed 35 pixels below the LEDs. */
	contents_switch[i].switch_y = contents_switch[i].light_y + 35;
	contents_switch[i].activity_status = 0;
	/* The default "last access time" is -1; this states that the switches
	   have not yet been activated. */
	contents_switch[i].last_access_time = -1;
    }

    /* Now we initialise the memory address switches. */
    for (i = 0; i < 8; i++) {
	/* The address LEDs and switches start 170 pixels in, and are spaced
	   at 30 pixel increments. */
	address_switch[i].light_x = address_switch[i].switch_x
	    = 170 + (i * 30);
	/* The address LEDs are placed 80 pixels below the top edge of the
	   computer box. */
	address_switch[i].light_y = BUFFER_HEIGHT + 80;
	/* The address switches are placed 35 pixels below the LEDs. */
	address_switch[i].switch_y = address_switch[i].light_y + 35;
	address_switch[i].activity_status = 0;
	address_switch[i].last_access_time = -1;
    }

    /* Next, we do the "awaiting input" LED. */
    /* The LED is placed 450 pixels in. */
    awaiting_input.light_x = 450;
    /* The LED is placed 40 pixels below the top edge of the computer box. */
    awaiting_input.light_y = BUFFER_HEIGHT + 40;
    awaiting_input.activity_status = 0;

    /* Then, we want to do the user input switch. */
    /* The LED and switch are placed on the same x coordinate as the "awaiting
       input" light. */
    user_input.light_x = user_input.switch_x = awaiting_input.light_x;
    /* The LED is placed 110 pixels below the top edge of the computer box. */
    user_input.light_y = BUFFER_HEIGHT + 110;
    /* The switch is placed 35 pixels below the LED. */
    user_input.switch_y = user_input.light_y + 35;
    /* We set the activity status of this just in case, even though we don't
       need it. */
    user_input.activity_status = 0;
    user_input.last_access_time = -1;

    /* Finally, we do the power switch, the "clear memory" switch and the
       "execute instructions" switch. */
    /* All of these LEDs and switches have the same x coordinate; 500 pixels
       in. */
    power_switch.light_x = clear_memory.light_x = execute_switch.light_x
	= power_switch.switch_x = clear_memory.switch_x 
	= execute_switch.switch_x = 500;
    /* The power switch is placed individually; the other y coordinates are
       based on pre-existing coordinates. */
    power_switch.light_y = BUFFER_HEIGHT + 20;
    power_switch.switch_y = power_switch.light_y + 30;
    clear_memory.light_y = address_switch[0].light_y + 10;
    clear_memory.switch_y = address_switch[0].switch_y;
    execute_switch.light_y = contents_switch[0].light_y;
    execute_switch.switch_y = contents_switch[0].switch_y;

    power_switch.activity_status = clear_memory.activity_status =
	execute_switch.activity_status = 0;
    power_switch.last_access_time = clear_memory.last_access_time =
	execute_switch.last_access_time = -1;
}

/* setup_screen: Sets graphics mode, then calls the setup_components() function
   to draw the screen. */
void setup_screen(void)
{
    /* Change video mode to 640x480 windowed */
    int ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    if (ret != 0) {
	allegro_message(allegro_error);
	return;
    }

    setup_components();
}

/* setup_components: Sets up all the individual graphical elements of the
   simulator. */
void setup_components(void)
{
    rectfill(screen, 0, 0, SCREEN_W, SCREEN_H, 0); /* Clear screen */

    /* Load bitmaps into memory */
    if (front_plate == NULL)
	front_plate = load_bitmap("front_plate.pcx", NULL);
    if (led_on == NULL)
	led_on = load_bitmap("led_on.pcx", NULL);
    if (led_off == NULL)
	led_off = load_bitmap("led_off.pcx", NULL);
    if (switch_on == NULL)
	switch_on = load_bitmap("switch_on.pcx", NULL);
    if (switch_off == NULL)
	switch_off = load_bitmap("switch_off.pcx", NULL);

    setup_front_plate();
    setup_accumulator();
    setup_memory_addresses();
    setup_memory_contents();
    setup_power_switch();
    setup_execute_switch();
    setup_clear_switch();
    setup_input_light();
    setup_uinput_switch();
}

/* setup_front_plate: Blits the front plate bitmap to the screen. */
void setup_front_plate(void)
{
    blit(front_plate, screen, 0, 0, 0, 115, SCREEN_W, 250);
}

/* setup_accumulator: Draws the accumulator lights in their default "off"
   state */
void setup_accumulator(void)
{
    int i;

    /* Blit the unlit LEDs to the screen in sequence. */
    for (i = 0; i < 12; i++)
	blit(led_off, screen, 0, 0, accumulator_led[i].light_x,
	     accumulator_led[i].light_y, LED_WIDTH, LED_HEIGHT);

    /* Print a label 5 pixels below the row of LEDs; 25 pixels below their
       top point. */
    textout_ex(screen, font, "ACCUMULATOR", 200,
	       accumulator_led[0].light_y + 25, 15, -1);
}

/* setup_memory_addresses: Draws the memory address lights and switches in their
   default "off" state */
void setup_memory_addresses(void)
{
    int i;

    /* Blit the unlit LEDs and switches to the screen in sequence. */
    for (i = 0; i < 8; i++) {
	blit(led_off, screen, 0, 0, address_switch[i].light_x,
	     address_switch[i].light_y, LED_WIDTH, LED_HEIGHT);
	blit(switch_off, screen, 0, 0, address_switch[i].switch_x,
	     address_switch[i].switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
    }

    /* Print a label 5 pixels below the row of LEDs; 25 pixels below their
       top point. */
    textout_ex(screen, font, "MEMORY ADDRESS", 200, 
	       address_switch[0].light_y + 25, 15, -1);
}

/* setup_memory_contents: Draws the address contents lights and switches in
   their default "off" state */
void setup_memory_contents(void)
{
    int i;

    /* Blit the unlit LEDs and switches to the screen in sequence. */
    for (i = 0; i < 12; i++) {
	blit(led_off, screen, 0, 0, contents_switch[i].light_x,
	     contents_switch[i].light_y, LED_WIDTH, LED_HEIGHT);
	blit(switch_off, screen, 0, 0, contents_switch[i].switch_x,
	     contents_switch[i].switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
    }

    /* Print a label 5 pixels below the row of LEDs; 25 pixels below their
       top point. */
    textout_ex(screen, font, "ADDRESS CONTENTS", 200, 
	       contents_switch[0].light_y + 25, 15, -1);
}

/* setup_power_switch: Draws the power toggle light and switch in their default
   "off" state */
void setup_power_switch(void)
{
    /* Blit the unlit LED and switch to the screen. */
    blit(led_off, screen, 0, 0, power_switch.light_x, power_switch.light_y,
	 LED_WIDTH, LED_HEIGHT);
    blit(switch_off, screen, 0, 0, power_switch.switch_x, power_switch.switch_y,
	 SWITCH_WIDTH, SWITCH_HEIGHT);

    /* Print a label 10 pixels beside the LED; 30 pixels to its leftmost point
       and 5 pixels below its top point. */
    textout_ex(screen, font, "POWER", power_switch.light_x + 30,
	       power_switch.light_y + 5, 15, -1);
}

/* setup_clear_switch: Draws the clear memory light and switch in their default
   "off" state */
void setup_clear_switch(void)
{
    /* Blit the unlit LED and switch to the screen. */
    blit(led_off, screen, 0, 0, clear_memory.light_x,
	 clear_memory.light_y, LED_WIDTH, LED_HEIGHT);
    blit(switch_off, screen, 0, 0, clear_memory.switch_x,
	 clear_memory.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);

    /* Print a label 10 pixels beside the LED; 30 pixels to its leftmost point
       and 5 pixels below its top point. */
    textout_ex(screen, font, "CLEAR", clear_memory.light_x + 30,
	       clear_memory.light_y + 5, 15, -1);
    textout_ex(screen, font, "MEMORY", clear_memory.light_x + 30,
	       clear_memory.light_y + 15, 15, -1);
}

/* setup_execute_switch: Draws the execute instructions light and switch in
   their default "off" state */
void setup_execute_switch(void)
{
    /* Blit the unlit LED and switch to the screen. */
    blit(led_off, screen, 0, 0, execute_switch.light_x,
	 execute_switch.light_y, LED_WIDTH, LED_HEIGHT);
    blit(switch_off, screen, 0, 0, execute_switch.switch_x,
	 execute_switch.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);

    /* Print a label 10 pixels beside the LED; 30 pixels to its leftmost point
       and 5 pixels below its top point. */
    textout_ex(screen, font, "EXECUTE", execute_switch.light_x + 30,
	       execute_switch.light_y + 5, 15, -1);
    textout_ex(screen, font, "INSTRUCTIONS", execute_switch.light_x + 30,
	       execute_switch.light_y + 15, 15, -1);
}

/* setup_input_light: Draws the input light in its default "off" state */
void setup_input_light(void)
{
    /* Blit the unlit LED to the screen. */
    blit(led_off, screen, 0, 0, awaiting_input.light_x,
	 awaiting_input.light_y, LED_WIDTH, LED_HEIGHT);

    /* Print a label 10 pixels below the LED; 30 pixels below its top point
       and 20 pixels left of its leftmost point. */
    textout_ex(screen, font, "AWAITING", awaiting_input.light_x - 20,
	       awaiting_input.light_y + 30, 15, -1);
    textout_ex(screen, font, "INPUT", awaiting_input.light_x - 20,
	       awaiting_input.light_y + 40, 15, -1);
}

/* setup_input_light: Draws the input light in its default "off" state */
void setup_uinput_switch(void)
{
    /* Blit the unlit LED and switch to the screen. */
    blit(led_off, screen, 0, 0, user_input.light_x,
	 user_input.light_y, LED_WIDTH, LED_HEIGHT);
    blit(switch_off, screen, 0, 0, user_input.switch_x,
	 user_input.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);

    /* Print a label 10 pixels below the switch; 45 pixels below its top point
       and 20 pixels left of its leftmost point. */
    textout_ex(screen, font, "USER", user_input.switch_x,
	       user_input.switch_y + 45, 15, -1);
    textout_ex(screen, font, "INPUT", user_input.switch_x,
	       user_input.switch_y + 55, 15, -1);
}

/* initialise_memory: zeroes the contents of all mailboxes and the
   accumulator */
void initialise_memory(void)
{
    int i;

    /* Set mailbox contents appropriately to clear memory */
    for (i = 0; i < MAILBOXES; i++) {
	mailbox[i] = 0;
    }

    /* Clear accumulator contents */
    accumulator = 0;
}

/* get_input: receives and resolves user input from the keyboard */
void get_input(void)
{
    if (key[KEY_P])
	toggle_power();

    if (power_switch.activity_status == 1) {
	/* Toggle address switches */
	if (key[KEY_Q])
	    toggle_address_switch(0);

	if (key[KEY_W])
	    toggle_address_switch(1);

	if (key[KEY_E])
	    toggle_address_switch(2);

	if (key[KEY_R])
	    toggle_address_switch(3);

	if (key[KEY_T])
	    toggle_address_switch(4);

	if (key[KEY_Y])
	    toggle_address_switch(5);

	if (key[KEY_U])
	    toggle_address_switch(6);

	if (key[KEY_I])
	    toggle_address_switch(7);

	/* Toggle contents switches */
	if (key[KEY_A])
	    toggle_contents_switch(0, 't');

	if (key[KEY_S])
	    toggle_contents_switch(1, 't');

	if (key[KEY_D])
	    toggle_contents_switch(2, 't');

	if (key[KEY_F])
	    toggle_contents_switch(3, 't');

	if (key[KEY_G])
	    toggle_contents_switch(4, 't');

	if (key[KEY_H])
	    toggle_contents_switch(5, 't');

	if (key[KEY_J])
	    toggle_contents_switch(6, 't');

	if (key[KEY_K])
	    toggle_contents_switch(7, 't');

	if (key[KEY_Z])
	    toggle_contents_switch(8, 't');

	if (key[KEY_X])
	    toggle_contents_switch(9, 't');

	if (key[KEY_C])
	    toggle_contents_switch(10, 't');

	if (key[KEY_V])
	    toggle_contents_switch(11, 't');

	/* Toggle clear memory switch */
	if (key[KEY_DEL])
	    toggle_clear_switch();

	/* Toggle execute instructions switch */
	if (key[KEY_ENTER])
	    toggle_execute_switch();
    }
}

/* toggle_power: toggles power from on to off and vice versa; flips the switch
   and light status appropriately. */
void toggle_power(void)
{
    int i;

    /* Check if the switch was accessed in the last 300 milliseconds */
    if (power_switch.last_access_time < 0 || 	(clock() / (double) CLOCKS_PER_MS) - 	power_switch.last_access_time >= 300) {
	/* Set the new access time */
	power_switch.last_access_time = clock() / (double) CLOCKS_PER_MS;

	if (power_switch.activity_status == 0) {
	    /* Turn on power, change power switch activity status */
	    power_switch.activity_status = 1;
	    /* Turn on light, flip switch up */
	    blit(led_on, screen, 0, 0, power_switch.light_x,
		 power_switch.light_y, LED_WIDTH, LED_HEIGHT);
	    blit(switch_on, screen, 0, 0, power_switch.switch_x,
		 power_switch.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
	} else {
	    /* Turn off power, change power switch activity status */
	    power_switch.activity_status = 0;
	    /* Clear memory, reset screen, set address counter to 0 */
	    initialise_memory();
	    setup_components();
	    address_counter = 0;
	    /* Set all of the contents and address switch activity statuses
	       back to 0 */
	    for (i = 0; i < 8; i++)
		address_switch[i].activity_status = 0;
	    for (i = 0; i < 12; i++)
		contents_switch[i].activity_status = 0;
	}
    }
}

/* toggle_address_switch: toggles the bit in the current address addressed by
 the switch from on to off and vice versa; flips the switch and light status
 appropriately. */
void toggle_address_switch(int toggle)
{
    /* Check if the switch was accessed in the last 300 milliseconds */
    if (address_switch[toggle].last_access_time < 0 || 	(clock() / (double) CLOCKS_PER_MS) - 	address_switch[toggle].last_access_time >= 300) {
	/* Set the new access time */
	address_switch[toggle].last_access_time =
	    clock() / (double) CLOCKS_PER_MS;

	if (address_switch[toggle].activity_status == 0) {
	    /* Change address switch activity status */
	    address_switch[toggle].activity_status = 1;
	    /* Turn on light, flip switch up */
	    blit(led_on, screen, 0, 0, address_switch[toggle].light_x,
		 address_switch[toggle].light_y, LED_WIDTH, LED_HEIGHT);
	    blit(switch_on, screen, 0, 0, address_switch[toggle].switch_x,
		 address_switch[toggle].switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
	} else {
	    /* Change address switch activity status */
	    address_switch[toggle].activity_status = 0;
	    /* Turn off light, flip switch down */
	    blit(led_off, screen, 0, 0, address_switch[toggle].light_x,
		 address_switch[toggle].light_y, LED_WIDTH, LED_HEIGHT);
	    blit(switch_off, screen, 0, 0, address_switch[toggle].switch_x,
		 address_switch[toggle].switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
	}

	/* Flip the bit of the program counter corresponding to the big-endian
	   position of the address switch */
	address_counter ^= 1 << (7 - toggle);
    }

    set_contents();
}

/* set_contents: Sets the positions of the memory contents switches correctly
   based on the current memory address */
void set_contents(void)
{
    int i;

    for (i = 0; i < 12; i++) {
	if (mailbox[address_counter] & (1 << (11 - i))) {
	    /* Change activity status appropriately */
	    contents_switch[i].activity_status = 1;
	    /* Turn on light, flip switch up */
	    blit(led_on, screen, 0, 0, contents_switch[i].light_x,
		 contents_switch[i].light_y, LED_WIDTH, LED_HEIGHT);
	    blit(switch_on, screen, 0, 0, contents_switch[i].switch_x,
		 contents_switch[i].switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
	} else {
	    /* Change activity status appropriately */
	    contents_switch[i].activity_status = 0;
	    /* Turn off light, flip switch down */
	    blit(led_off, screen, 0, 0, contents_switch[i].light_x,
		 contents_switch[i].light_y, LED_WIDTH, LED_HEIGHT);
	    blit(switch_off, screen, 0, 0, contents_switch[i].switch_x,
		 contents_switch[i].switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
	}
    }
}

/* toggle_contents_switch: Flips the switch and light status of the switch
 appropriately; depending on the mode, it may also toggle the bit in either
 the current memory address's contents or the temporary input storage buffer
 based on the switch. */
void toggle_contents_switch(int toggle, char mode)
{
    /* Check if the switch was accessed in the last 300 milliseconds */
    if (contents_switch[toggle].last_access_time < 0 || 	(clock() / (double) CLOCKS_PER_MS) - 	contents_switch[toggle].last_access_time >= 300) {
	/* Set the new access time */
	contents_switch[toggle].last_access_time =
	    clock() / (double) CLOCKS_PER_MS;

	if (contents_switch[toggle].activity_status == 0) {
	    /* Change activity status appropriately */
	    contents_switch[toggle].activity_status = 1;
	    /* Turn on light, flip switch up */
	    blit(led_on, screen, 0, 0, contents_switch[toggle].light_x,
		 contents_switch[toggle].light_y, LED_WIDTH, LED_HEIGHT);
	    blit(switch_on, screen, 0, 0, contents_switch[toggle].switch_x,
		 contents_switch[toggle].switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
	} else {
	    /* Change activity status appropriately */
	    contents_switch[toggle].activity_status = 0;
	    /* Turn off light, flip switch down */
	    blit(led_off, screen, 0, 0, contents_switch[toggle].light_x,
		 contents_switch[toggle].light_y, LED_WIDTH, LED_HEIGHT);
	    blit(switch_off, screen, 0, 0, contents_switch[toggle].switch_x,
		 contents_switch[toggle].switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
	}

	/* If mode is 't', flip the bit in the address contents corresponding to
	   the big-endian position of the switch.
	   If mode is 'a', do this for the bit in the temporary input store. */
	if (mode == 't')
	    mailbox[address_counter] ^= 1 << (11 - toggle);
	if (mode == 'a')
	    input_temp ^= 1 << (11 - toggle);
    }
}

/* toggle_clear_switch: Clears the contents of the mailbox contents; flips the
   clear switch up and down and turns the light on and off. */
void toggle_clear_switch(void)
{
    int i;

    /* Turn on light, flip switch up. Pause for effect */
    blit(led_on, screen, 0, 0, clear_memory.light_x,
	 clear_memory.light_y, LED_WIDTH, LED_HEIGHT);
    blit(switch_on, screen, 0, 0, clear_memory.switch_x,
	 clear_memory.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
    rest(150);

    /* Clear contents of all mailboxes */
    for (i = 0; i < MAILBOXES; i++) {
	mailbox[i] = 0;
    }

    /* Change positions of address contents switches */
    set_contents();

    /* Turn off light, flip switch up */
    blit(led_off, screen, 0, 0, clear_memory.light_x,
	 clear_memory.light_y, LED_WIDTH, LED_HEIGHT);
    blit(switch_off, screen, 0, 0, clear_memory.switch_x,
	 clear_memory.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
}

/* toggle_execute_switch: Calls the execute_instructions() function; keeps the
   switch flipped down and the light turned on until that function has
   finished. */
void toggle_execute_switch(void)
{
    /* Check if the switch was accessed in the last 300 milliseconds */
    if (execute_switch.last_access_time < 0 || 	(clock() / (double) CLOCKS_PER_MS) - 	execute_switch.last_access_time >= 300) {
	/* Set the new access time */
	execute_switch.last_access_time =
	    clock() / (double) CLOCKS_PER_MS;

	/* Turn on light, flip switch down. Pause for effect */
	blit(led_on, screen, 0, 0, execute_switch.light_x,
	 execute_switch.light_y, LED_WIDTH, LED_HEIGHT);
	blit(switch_on, screen, 0, 0, execute_switch.switch_x,
	 execute_switch.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
	rest(200);

        execute_instructions();

	/* Turn off light, flip switch up. */
	blit(led_off, screen, 0, 0, execute_switch.light_x,
	 execute_switch.light_y, LED_WIDTH, LED_HEIGHT);
	blit(switch_off, screen, 0, 0, execute_switch.switch_x,
	 execute_switch.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
    }
}

/* execute_instructions: Splits instructions into op codes and operands, then
   executes those instructions in accordance with the modified hexadecimal LMC
   rules of execution. */
void execute_instructions(void)
{
    int opcode = 0, operand = 0;
    int pc = 0;
    int i;
    FILE *ofp = fopen("lmc_output", "a");

    accumulator = 0;
    do {
	opcode = mailbox[pc] / (OP_SPLIT + 1);
	operand = mailbox[pc] & OP_SPLIT;
	/* Increment the program counter in advance; if it's a jump instruction,
	 the PC will be set manually. */
	++pc;

	switch(opcode) {
	case 0x1:
	    accumulator += mailbox[operand];
	    break;
	case 0x2:
	    accumulator -= mailbox[operand];
	    break;
	case 0x3:
	    mailbox[operand] = accumulator;
	    break;
	case 0x4:
	    break; /* Treat as NOP */
	case 0x5:
	    accumulator = mailbox[operand];
	    break;
	case 0x6:
	    pc = operand;
	    break;
	case 0x7:
	    /* If accumulator is zero, branch; otherwise, progress to next
	       instruction - treat as NOP. */
	    if (accumulator == 0)
		pc = operand;
	    break;
	case 0x8:
	    /* If accumulator is greater than or equal to zero, branch;
	       otherwise, progress to next instruction - treat as NOP. */
	    if (accumulator >= 0)
		pc = operand;
	    break;
	case 0x9:
	    if (operand == 0x1) {
		get_program_input();
		accumulator = input_temp;
	    } else if (operand == 0x2) {
		fprintf(ofp, "%3x\n", accumulator);
	    }
	    break;
	case 0x0:
	    set_accumulator(); /* Display accumulator contents */
	    fprintf(ofp, "----- INSTRUCTIONS COMPLETE -----\n");
	    fclose(ofp);
	    return;
	default:
	    break; /* All op codes outside of 0 - 9 are undefined.
		    * Treat as NOP. */
	}

	set_accumulator(); /* Display accumulator contents */

	/* Pause for effect, to enable accumulator lights to be seen in action.
	   May be changed or removed for a faster or slower simulation. */
	rest(INSTRUCTION_REST);

	if (key[KEY_ESC])
	    return; /* Allow the user to exit, e.g. from an infinite loop */
    } while (opcode != 0);
}

/* set_accumulator: Set the status of the accumulator lights based on the
   active bits in the accumulator "register" */
void set_accumulator(void)
{
    int i;

    /* Set accumulator lights based on the active bits in the accumulator */
    for (i = 0; i < 12; i++) {
	if (accumulator & 1 << (11 - i))
	    blit(led_on, screen, 0, 0, accumulator_led[i].light_x,
		 accumulator_led[i].light_y, LED_WIDTH, LED_HEIGHT);
	else
	    blit(led_off, screen, 0, 0, accumulator_led[i].light_x,
		 accumulator_led[i].light_y, LED_WIDTH, LED_HEIGHT);
    }
}

/* get_program_input: Allows entry of data into the temporary input storage
   buffer using the memory contents switches. */
void get_program_input(void)
{
    int i;

    /* Clear temporary input storage buffer */
    input_temp = 0;

    /* Clear all contents switches */
    for (i = 0; i < 12; i++) {
	contents_switch[i].activity_status = 0;
	blit(led_off, screen, 0, 0, contents_switch[i].light_x,
	     contents_switch[i].light_y, LED_WIDTH, LED_HEIGHT);
	blit(switch_off, screen, 0, 0, contents_switch[i].switch_x,
	     contents_switch[i].switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
    }

    /* Turn on "awaiting input" LED */
    blit(led_on, screen, 0, 0, awaiting_input.light_x, awaiting_input.light_y,
	 LED_WIDTH, LED_HEIGHT);

    /* Basically, keep taking contents entries while the Space key isn't pressed
       and the last access time period of 300 milliseconds hasn't been met. Yes,
       it's a very complex test. */
    for(;;) {
	/* Toggle contents switches */
	if (key[KEY_A])
	    toggle_contents_switch(0, 'a');

	if (key[KEY_S])
	    toggle_contents_switch(1, 'a');

	if (key[KEY_D])
	    toggle_contents_switch(2, 'a');

	if (key[KEY_F])
	    toggle_contents_switch(3, 'a');

	if (key[KEY_G])
	    toggle_contents_switch(4, 'a');

	if (key[KEY_H])
	    toggle_contents_switch(5, 'a');

	if (key[KEY_J])
	    toggle_contents_switch(6, 'a');

	if (key[KEY_K])
	    toggle_contents_switch(7, 'a');

	if (key[KEY_Z])
	    toggle_contents_switch(8, 'a');

	if (key[KEY_X])
	    toggle_contents_switch(9, 'a');

	if (key[KEY_C])
	    toggle_contents_switch(10, 'a');

	if (key[KEY_V])
	    toggle_contents_switch(11, 'a');

	/* I recognise that wrapping this test up in an infinite loop is bad
	   practice, but I couldn't get the last access time to work in the
	   main test of the while loop, and the key bounce issue from last
	   time was plaguing this part of the program again. */
	if (key[KEY_SPACE] && (user_input.last_access_time < 0 || 	(clock() / (double) CLOCKS_PER_MS) - 			       user_input.last_access_time >= 300))
	    break;

	/* We also need a way to escape from this part of the program
	   quickly, in order to allow exiting from all states in the program. */
	if (key[KEY_ESC])
	    return;
    }

    /* Set the last access time for the user input switch */
    user_input.last_access_time = clock() / (double) CLOCKS_PER_MS;

    /* Turn on light, flip switch down. Pause for effect */
    blit(led_on, screen, 0, 0, user_input.light_x,
	 user_input.light_y, LED_WIDTH, LED_HEIGHT);
    blit(switch_on, screen, 0, 0, user_input.switch_x,
	 user_input.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);
    rest(200);

    /* Turn off light, flip switch up. */
    blit(led_off, screen, 0, 0, user_input.light_x,
	 user_input.light_y, LED_WIDTH, LED_HEIGHT);
    blit(switch_off, screen, 0, 0, user_input.switch_x,
	 user_input.switch_y, SWITCH_WIDTH, SWITCH_HEIGHT);

    /* Turn off the "awaiting input" LED */
    blit(led_off, screen, 0, 0, awaiting_input.light_x, awaiting_input.light_y,
	 LED_WIDTH, LED_HEIGHT);

    /* Reset the contents switches */
    set_contents();
}
Advertisements

One Response

  1. Hey cutie from a young womanreader contunue the fab website

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: