Results 1 to 19 of 19
  1. #1
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default hELP WITH A PROCESSOR SIMULATION

    Hello All,

    Im trying to create a simple processor simulation but Im having the toughest time storing my file and then creating the Algorithms needed for my PC, AC, and MAR registers.

    Heres the instructions for the algorithm:

    00 (DATA) Not an instruction. Memory location holds some data value.
    01 LOAD Load memory into AC register
    02 ADD Add memory into AC register
    03 SUB Subtract memory from AC register
    04 JUMP Set PC Register to memory
    05 MOV Copy AC into Memory
    15 HALT Stop all execution. Your program ends here. Output the current state of memory and all register values.

    *Numbers on the side is the opcode
    The maximum value for an opcode is 15. The maximum value for a memory address or a memory value is 9999.

    Constraints:

    The only available memory is what is given in the initial text file.
    All lines are separated by newline characters (\n). All characters in the textfile are encoded using standard ASCII.
    There will not be any invalid instructions in memory.
    Upon starting the PC register will always point to valid memory.
    The PC register will be incremented by one after each instruction, except when you encounter a JUMP. Just set the PC register to the proper value when you encounter a JUMP.
    No JUMP instruction will ever take you to non-executable memory.
    You should interpret all integer values in the text file as base-10 (decimal) numbers.
    When you reach a HALT instruction, you are done executing.
    A MOV instruction leaves the AC intact.
    The MAR changes in accordance with the memory address last accessed by a LOAD, ADD, SUB, JUMP, or MOV instruction.

    Code I have thus far:

    import java.io.*;
    class Simulator
    {
    public static void main(String args[])
    {
    try{
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream("memory.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null) {
    // Print the content on the console
    System.out.println (strLine);
    }
    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }
    }

    SOMEONE PLZ HELP ME!!!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Spell words out in full. Using plz makes you look childish.
    Do not use caps. Nobody likes being shouted at.
    Help with what? Ask a specific question and get a specific answer.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Another thing do not double post.

  4. #4
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    thank you for all of your Critique I will take it into consideration on my next post. my first question would be based on all the information provided above how would i store the data that is being read into java from the text file Im using (memory.txt)

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Can you post the contents of memory.txt?

  6. #6
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    the contents of memory.txt:

    0100:00:2039
    0101:00:0293
    0102:00:1349
    0103:00:0032
    0104:00:6002
    1105:01:0101
    1106:02:0104
    1107:02:0102
    1108:03:0101
    1109:04:1113
    1110:02:0100
    1111:02:0102
    1112:04:0100
    1113:05:0101
    1114:15:0000

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Since this is a simulation you would use variables and/or arrays to represent memory and registers. Read the entire file and store the instructions somewhere. Then begin processing the instructions which will require you to update your "memory" and "registers".

  8. #8
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    ok thanks. im working on it right now. ill post what i have in a bit.

  9. #9
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    A small bit of what im doing but im stuck. im trying to take each line thats being read in, assign it to pc:mar:ac and execute the instructions but i honestly dont know how to do it.

    public static void main(String args[])
    {
    try{
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream("memory.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null) {
    int pc, ac, mar;
    strLine = pc:mar:ac;

    private final int maxMAR = 15;

    public void Count ()
    {
    if (mar < maxMAR) mar++;
    else return HALT;
    }

  10. #10
    zbest is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Lolol Njit cs332 wed night class? its cool I need help too :)

  11. #11
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    zbest, how far have you gotten with the program?

  12. #12
    zbest is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    I'm further behind then you are dude, honestly programing isnt my strongest suit

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Not much of your code makes any sense.
    Java Code:
    while ((strLine = br.readLine()) != null) {  // ok upto here
    int pc, ac, mar;  // each time around the loop you get new variables. You could do this but I would declare them once and just update their values
    strLine = pc:mar:ac; // you read a line into strLine above and then throw it away here. Plus this is not even legal syntax.
    
    private final int maxMAR = 15; // cannot declare a private int here. Plus what is for?
    
    public void Count ()  // not legal, cannot declare a method inside the main method. Plus what is it for?
    {
    if (mar < maxMAR) mar++;
    else return HALT; // what is HALT? Plus the method is void so you cannot return anything
    }
    I'm sorry but this is a complete mess. It appears you do not have much idea of what you are doing. I suggest talking to your teacher or a tutor and getting some one-on-one help.

  14. #14
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    i agree. im just trying out different method. i have a different version of the program written down.

    import java.io.*;
    class Simulator
    {
    public static void main(String args[])
    {

    try {

    FileOutputStream fos = new FileOutputStream("memory.txt");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    //Flush and close the ObjectOutputStream
    oos.flush();
    oos.close();

    //Now that the data is saved. Its time to bring it back and execute instructions
    FileInputStream fstream = new FileInputStream("memory.txt");
    ObjectInputStream ois = new ObjectInputStream(fstream);




    ois.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }


    But with this one I still need to set variables for my memory and registers however i dont quite know how to do that. any suggestions?

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    0100:00:2039

    I'm still unsure what the instructions should do. I'm guessing this means store 2039 in memory location 100 but not 100% sure. I would speak to your lecturer to clarify.

    Also insturctions say that PC should increment by 1 but after 0104 it jumps 1001 to 1105. Seek clarification.

    You need to look at each instruction, spilt it into the three parts. Use String.split on :. Then you know what instruction is to be performed (the middle value) and which registers/memory locations need to accessed and values retrieved or set.

    I cannot stress enough that if you are not understanding this assignment go and talk to the lecturer as he/she knows and I'm only guessing.

  16. #16
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    thank you for all of your help. i will definitely be going to speck to my professor tomorrow. we just came off of spring break and he was unreachable. but once again thanks for the help. if anything changes with the program ill post it.

  17. #17
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    oh and by the way this is the output he has:

    CPU halted.
    ------------
    0100:00:2039
    0101:00:7351
    0102:00:1349
    0103:00:0032
    0104:00:6002
    1105:01:0101
    1106:02:0104
    1107:02:0102
    1108:03:0101
    1109:04:1113
    1110:02:0100
    1111:02:0102
    1112:04:0100
    1113:05:0101
    1114:15:0000
    ------------
    PC = 1113
    AC = 7351
    MAR = 101
    ------------

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Quan567 View Post
    oh and by the way this is the output he has:

    CPU halted.
    ------------
    0100:00:2039
    0101:00:7351
    0102:00:1349
    0103:00:0032
    0104:00:6002
    1105:01:0101
    1106:02:0104
    1107:02:0102
    1108:03:0101
    1109:04:1113
    1110:02:0100
    1111:02:0102
    1112:04:0100
    1113:05:0101
    1114:15:0000
    ------------
    PC = 1113
    AC = 7351
    MAR = 101
    ------------
    I don't think that is correct: the 'halt' instrution (15) is stored at memory location 1114 so the machine can't have halted at PC = 1113. It's a 'famous' off-by-one-error.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    Quan567's Avatar
    Quan567 is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default

    hello everyone, I talked to my TA today because he was the one that actually created the assignment and when I brought up the points that you guys had made about the programs instructions he said they were perfectly fine. So with that being said this is what I have so far; I know there are syntax errors, I need help debugging.


    import java.io.*;
    import java.util.*;
    class Simulation
    {
    public static void main(String args[])
    {
    try{
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream("memory.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;

    int pc = strLine.substring(0,3);
    int mar = strLine.substring(4,5);
    int ac = strLine.substring(6,9);

    //Read File Line By Line
    while ((strLine = br.readLine()) != null) {

    class HashTable{

    int pc, mar, ac;

    HashTable <String> mem = new HashTable <String>();
    mem.pc(index, line);
    mem.get(pc);

    if(mar = 01){
    mem.get(pc);
    ac = ac;
    pc++;

    }else if(mar = 02){
    mem.get(pc);
    ac = ac+pc;
    pc++;

    } else if(mar = 03){
    mem.get(pc);
    ac = ac-pc;
    pc++;

    }else if(mar = 04){
    mem.get(pc);
    pc = ac;
    pc++;

    } else if(mar = 05){
    mem.get(pc);
    ac = ac;
    pc++;

    }else if(mar = 15){
    System.exit(0);
    }
    }
    }
    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }

    // Print the content on the console
    System.out.println (strLine);
    }
    }

Similar Threads

  1. Create a Processor Simulation
    By Quan567 in forum Advanced Java
    Replies: 2
    Last Post: 03-23-2011, 01:28 AM
  2. Java Picture Processor: PGM files and Arrays
    By snow_mac in forum New To Java
    Replies: 0
    Last Post: 11-30-2010, 01:16 PM
  3. Simulation in Java
    By Eranga in forum Advanced Java
    Replies: 15
    Last Post: 08-15-2010, 04:32 AM
  4. beehive simulation
    By BlueF4re in forum New To Java
    Replies: 2
    Last Post: 12-02-2009, 08:31 AM
  5. Address space with 32-bit processor
    By GlideKensington in forum New To Java
    Replies: 1
    Last Post: 04-06-2009, 07:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •