Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
PDP8-Simulator - main.cc

main.cc

Caricato da: Matthew
Scarica il programma completo

  1. /*
  2.  *  main.cc
  3.  *
  4.  *      Assembler for PDP-8 Virtual Machine
  5.  *
  6.  *  Created on: Mar 16, 2010
  7.  *      Author: matthew
  8.  */
  9.  
  10. #include <iostream>
  11. #include <fstream>
  12. #include <stdlib.h>
  13. #include <vector>
  14. #include <string.h>
  15. #include <sstream>
  16. #include "myLib.h"
  17. using namespace std;
  18.  
  19.  
  20. void compile(vector<instruction> &code, vector<message> &report)
  21. {
  22.         ostringstream output;
  23.         int n,bit4, bit3=0;
  24.         string op_code;
  25.  
  26.         //Check START
  27.         if(code[0].word[0]!="START")
  28.         {
  29.                 c_error(report, 0, "START expected.");
  30.         }
  31.  
  32.         //Compile code
  33.         for(int a=1; a<code.size()-1; a++)
  34.         {
  35.                 //Set operation code
  36.                 if(code[a].word[0]=="AND")
  37.                 {
  38.                         op_code="000";
  39.                 }
  40.                 else if(code[a].word[0]=="TAD")
  41.                 {
  42.                         op_code="001";
  43.                 }
  44.                 else if(code[a].word[0]=="ISZ")
  45.                 {
  46.                         op_code="010";
  47.                 }
  48.                 else if(code[a].word[0]=="DCA")
  49.                 {
  50.                         op_code="011";
  51.                 }
  52.                 else if(code[a].word[0]=="JMS")
  53.                 {
  54.                         op_code="100";
  55.                 }
  56.                 else if(code[a].word[0]=="JMP")
  57.                 {
  58.                         op_code="101";
  59.                 }
  60.                 else if(code[a].word[0]=="OUT")
  61.                 {
  62.                         op_code="110";
  63.                 }
  64.                 else if(code[a].word[0]=="HLT")
  65.                 {
  66.                         output<<"111";
  67.                         output<<"110000010";
  68.                         continue;
  69.                 }
  70.                 else if(code[a].word[0]=="CLA")
  71.                 {
  72.                         output<<"111";
  73.                         output<<"010000000";
  74.                         continue;
  75.                 }
  76.                 else if(code[a].word[0]=="CMA")
  77.                 {
  78.                         output<<"111";
  79.                         output<<"000100000";
  80.                         continue;
  81.                 }
  82.                 else if(code[a].word[0]=="IAC")
  83.                 {
  84.                         output<<"111";
  85.                         output<<"000000001";
  86.                         continue;
  87.                 }
  88.                 else if(code[a].word[0]=="CLL")
  89.                 {
  90.                         output<<"111";
  91.                         output<<"001000000";
  92.                         continue;
  93.                 }
  94.                 else if(code[a].word[0]=="CML")
  95.                 {
  96.                         output<<"111";
  97.                         output<<"000010000";
  98.                         continue;
  99.                 }
  100.                 else if(code[a].word[0]=="RAR")
  101.                 {
  102.                         output<<"111";
  103.                         output<<"000001000";
  104.                         continue;
  105.                 }
  106.                 else if(code[a].word[0]=="RAL")
  107.                 {
  108.                         output<<"111";
  109.                         output<<"000000100";
  110.                         continue;
  111.                 }
  112.                 else if(code[a].word[0]=="RTR")
  113.                 {
  114.                         output<<"111";
  115.                         output<<"000001010";
  116.                         continue;
  117.                 }
  118.                 else if(code[a].word[0]=="RTL")
  119.                 {
  120.                         output<<"111";
  121.                         output<<"000000110";
  122.                         continue;
  123.                 }
  124.                 else if(code[a].word[0]=="SZA")
  125.                 {
  126.                         output<<"111";
  127.                         output<<"100100000";
  128.                         continue;
  129.                 }
  130.                 else if(code[a].word[0]=="SZL")
  131.                 {
  132.                         output<<"111";
  133.                         output<<"100011000";
  134.                         continue;
  135.                 }
  136.  
  137.                 //Return error if the operation is unknown
  138.                 else
  139.                 {
  140.                         c_error(report, a, code[a].word[0]+" unknown instruction.");
  141.                         continue;
  142.                 }
  143.  
  144.                         //Check number of arguments
  145.                         if(code[a].word.size()!=2)
  146.                         {
  147.                                 c_error(report, a, code[a].word[0]+" requires 1 argument.");
  148.                                 continue;
  149.                         }
  150.  
  151.                         //Check if the reference is direct and set bit 3
  152.                         if(code[a].word[1][0]=='$')bit3=1;
  153.                         else bit3=0;
  154.                         n = atoi(code[a].word[1].c_str()+bit3);
  155.  
  156.                         //TODO: create variables and handle direct addresses
  157.                         //Check if the address is valid
  158.                         if((n>0 && n<129 && n>>0))
  159.                         {
  160.                                 //check if the address is in the same page as the instruction
  161.                                 bit4 = bit_4(code[a].line, n);
  162.                                 if(bit4==-1)
  163.                                         c_error(report, a, "this operation cannot directly access address ");
  164.  
  165.                                 //Now assembly instruction
  166.                                 output<<op_code;
  167.                                 output<<bit3;
  168.                                 output<<bit4;
  169.                                 output<<binary(n);
  170.                         }
  171.                         //Report an error if not
  172.                         else
  173.                         {
  174.                                 c_error(report, a, code[a].word[1]+" invalid address.");
  175.                         }
  176.  
  177.         }
  178.         //Check END
  179.         if(code[code.size()-1].word[0]!="END")
  180.         {
  181.                 c_error(report, code.size()-1, "END expected.");
  182.         }
  183.         if(report.size()==0)
  184.         {
  185.                 ofstream binfile;
  186.                 binfile.open("bin.txt");
  187.                 if(binfile.good())
  188.                 {
  189.                         binfile<<output.str();
  190.                         debug_message("Binary file successfully assembled.");
  191.                 }
  192.                 else
  193.                 {
  194.                         error("Cannot create binary file file.");
  195.                 }
  196.                 binfile.close();
  197.         }
  198. }
  199.  
  200.  
  201. int main()
  202. {
  203.         ifstream sourcefile;
  204.         ofstream binfile;
  205.         int linenum=0;
  206.         vector<message>report;
  207.         vector<instruction>code;
  208.  
  209.         //Open source file
  210.         sourcefile.open("source.txt");
  211.         if(sourcefile.good())
  212.         {
  213.                 debug_message("Source file successfully opened.");
  214.         }
  215.         else
  216.         {
  217.                 error("Cannot open source file.");
  218.         }
  219.  
  220.         //Read source file and create source code structure
  221.         while(!sourcefile.eof())
  222.         {
  223.                 char line[50], *command;
  224.                 bool flag=false;
  225.                 instruction i;
  226.  
  227.                 //Read line from source file
  228.                 sourcefile.getline(line, 50);
  229.  
  230.                 //Split string into words
  231.                 command = strtok (line," \t");
  232.                 if(command!=NULL)flag=true;
  233.                 while (command != NULL)
  234.                 {
  235.                         //Create instruction
  236.                         i.word.push_back(command);
  237.                     command = strtok (NULL, " \t");
  238.                 }
  239.                 //Flag ignores blank lines
  240.                 if(flag)
  241.                 {
  242.                         //Add line number to instruction
  243.                         i.line=linenum++;
  244.  
  245.                         //Add instruction to source code
  246.                         code.push_back(i);
  247.                 }
  248.         }
  249.  
  250.         //Close source file
  251.         sourcefile.close();
  252.  
  253.         cout<<endl<<"SOURCE CODE:"<<endl<<endl;
  254.         //Compile source
  255.         compile(code, report);
  256.  
  257.         //Print code
  258.         for(int a=0; a<code.size(); a++)
  259.         {
  260.                 cout<<code[a].line<<" ";
  261.                 for(int b=0; b<code[a].word.size(); b++)
  262.                 {
  263.                         cout<<code[a].word[b]<< " ";
  264.                 }
  265.                 cout<<endl;
  266.         }
  267.         show_report(report);
  268.  
  269.         return 0;
  270. }