Simulating a "brookshear machine"

This is a discussion on Simulating a "brookshear machine" within the C Programming forums, part of the General Programming Boards category; Hi everyone, can someone please help me in understanding how to increment the pc counter and where to put my ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    6

    Simulating a "brookshear machine"

    Hi everyone, can someone please help me in understanding how to increment the pc counter and where to put my printf's in my code. This assignment is giving me some trouble. heres the question.

    Your program steps though the machine code, one instruction at a time. Before each instruction, your program prints out a line of text that describes the current contents of the Program Counter, the instruction to be executed and the registers.
    Each time an instruction is executed your program alters the Program Counter, Registers and Memory accordingly.

    The format of the output line is:

    PC INST [R0 R1 R2 R3 R4 R5 R6 R7 R8 R9 RA RB RC RD RE RF]

    Where PC is the program counter, INST is the current instruction and R0 through to RF are the value of the sixteen registers. Each field is output as a 2 or 4 digit hexadecimal value (use the X format specifier to get capital letters) with a leading 0 if necessary.

    Output continues (with no user intervention) until the Halt instruction is executed, at which time your program also stops.
    A Halt instruction will occur in all test programs.

    Example: if a3.txt contains:

    B404
    239A
    2412
    5345
    350C
    C000

    Then the output is:

    00 B404 [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00]
    04 2412 - [00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00]
    06 5345 - [00 00 00 00 12 00 00 00 00 00 00 00 00 00 00 00]
    08 350C - [00 00 00 00 12 12 00 00 00 00 00 00 00 00 00 00]
    0A C000 - [00 00 00 00 12 12 00 00 00 00 00 00 00 00 00 00]

    Use unsigned char's for your PC, registers and memory.

    When reading the a3.txt file, be careful with alignment problems in the scanf-like functions. First read each line into an unsigned int variable, then extract out the two bytes using bit-wise operators and store them in your memory array.

    this is what i have so far
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
        
        int unsigned r[16]={0};
        int unsigned mem[255];
        int mask1=0xFF00, mask2=0X00FF;
        int i=0;
        unsigned int temp=0;
        
        
        FILE *f;
        f=fopen("a3.txt", "r");
        
        while(fscanf(f, "%x", &temp)!= EOF){   
            mem[i]=(temp & mask1)>>8;
            mem[i+1]=(temp&mask2);
            i=i+2;
        }
        
        int a=0,b=0,c=0,d=0,cd=0;
        unsigned char pc=0;
        
        
        //i tried doing printf's below but the program counter(pc) doesnt increment so i end up with the same output easch time
        /*printf("%02X %X%02X - [", pc,mem[pc],mem[pc+1]);
        for(int j=0; j<15; j++){
            printf("%02X ", r[j]);
        }
        printf("%02X]\n", r[15]);
    */ 
    
        while (mem[pc]!=0xC0){
            a = (mem[pc] & 0xF0)>>4;
            b = (mem[pc] & 0xF);
            c = (mem[pc+1] & 0xF0)>>4;
            d = (mem[pc+1] & 0xF);
            cd = (mem[pc+1]);
        
            pc=pc+2;
            
            if(a==0x01){
                r[b]=mem[cd]; 
            }
        
            if(a==0x02){
                r[b]=cd;
            }
        
            if(a==0x03){
                r[cd]=mem[b];
            }
        
            if(a==0x04){
                r[d]=r[c];
            }
        
            if(a==0x05){
                r[d]=r[b]+r[c];
            }
            
            if(a==0x07){
                r[d]=r[b] | r[c];
            }
        
            if(a==0x08){
                r[d]=r[b] & r[c];
            }
        
            if(a==0x09){
                r[d]=r[b] ^ r[c];
            }
        
            //i am unsure of this rotate beneath. can someone please tell me what im doing wrong.
            if(a==0x0A){                
                for(int j=0; j<d; j++){
                    unsigned int least = r[b] & 0x01;
                    unsigned int most = least << 7;
                    r[b] = r[b] >> r[d];
                    r[b] = r[b] | most;
                }
            }
        
                if(a==0x0B){
                    if(r[b]==r[0]){
                        pc=cd;
                    }
                }
            }
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Rotating is simple, you just need:
    Code:
    number = 123;
    rotBits = 3;
    
    /* Left Rotation */
    number = (number << rotBits) | (number >> (sizeof(number)*CHAR_BITS - rotBits));
    /* Right Rotation */
    number = (number >> rotBits) | (number << (sizeof(number)*CHAR_BITS - rotBits));
    EDIT: The above code only works for "rotBits" >= 0

    About your problem, sorry but I don't know about this "brookshear machine". If you asked about Kenbak-1, I could answer. Are all of its instructions the same size?
    Last edited by GReaper; 04-30-2012 at 05:12 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    im having a hard time understanding whats going on in your rotates. could you explain it a bit further for me please?
    All the instructions are the same size, they are below.

    Brookshear machine instructions

    1RXY Load register R with the value at memory address XY

    2RXY Load register R with the value XY

    3RXY Store the value in register R at memory address XY

    40RS Copy/move the value in register R to register S

    5RST Add the values in registers R and S and put the answer in register T

    7RST Bit-wise OR the values in registers R and S and put the answer in register T

    8RST Bit-wise AND the values in registers R and S and put the answer in register T

    9RST Bit-wise XOR the values in registers R and S and put the answer in register T

    AR0X Rotate the contents of register R X times to the right

    BRXY Jump to the instruction located at memory address XY if the value in register R is equal to the value in register 0 (i.e. change the PC to XY)

    C000 Halt

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    When you're rotating to the left, you want to save the left-most bits, by moving them to the right-most place. Vice-versa for right rotation.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulating a "Mouse Click"
    By The_Krahe in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2005, 11:45 AM
  2. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  3. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM
  4. Vending "machine" C++ code help
    By sgara in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2002, 04:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21