OpenCores
URL https://opencores.org/ocsvn/hpc-16/hpc-16/trunk

Subversion Repositories hpc-16

[/] [hpc-16/] [trunk/] [impl0/] [asm/] [HPC16_asm.py] - Blame information for rev 18

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 18 umairsiddi
#!/usr/bin/python
2
#--------------------------------------------------------------
3
#-- HPC-16 Assembler
4
#--------------------------------------------------------------
5
#-- project: HPC-16 Microprocessor
6
#--
7
#-- usage: HPC16_asm.py <input_file>
8
#--
9
#-- 
10
#--
11
#-- Author: M. Umair Siddiqui (umairsiddiqui@opencores.org)
12
#---------------------------------------------------------------
13
#------------------------------------------------------------------------------------
14
#--                                                                                --
15
#--    Copyright (c) 2015, M. Umair Siddiqui all rights reserved                   --
16
#--                                                                                --
17
#--    This file is part of HPC-16.                                                --
18
#--                                                                                --
19
#--    HPC-16 is free software; you can redistribute it and/or modify              --
20
#--    it under the terms of the GNU Lesser General Public License as published by --
21
#--    the Free Software Foundation; either version 2.1 of the License, or         --
22
#--    (at your option) any later version.                                         --
23
#--                                                                                --
24
#--    HPC-16 is distributed in the hope that it will be useful,                   --
25
#--    but WITHOUT ANY WARRANTY; without even the implied warranty of              --
26
#--    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               --
27
#--    GNU Lesser General Public License for more details.                         --
28
#--                                                                                --
29
#--    You should have received a copy of the GNU Lesser General Public License    --
30
#--    along with HPC-16; if not, write to the Free Software                       --
31
#--    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   --
32
#--                                                                                --
33
#------------------------------------------------------------------------------------
34
 
35
from __future__ import print_function
36
import sys
37
import os
38
import re
39
from antlr4 import *
40
from HPC16Lexer import HPC16Lexer
41
from HPC16Parser import HPC16Parser
42
from MyHPC16Listener import MyHPC16Listener
43
from antlr4.error.ErrorStrategy import BailErrorStrategy
44
from antlr4.error.Errors import *
45
 
46
def start():
47
  if len(sys.argv) < 2:
48
    usage()
49
    sys.exit(1)
50
  input_stream = FileStream(sys.argv[1])
51
  a, _ = os.path.splitext(os.path.basename(sys.argv[1]))
52
  fout_name = a + "_asm_out.txt"
53
 
54
  fout = open(fout_name, 'w')
55
 
56
  lexer = HPC16Lexer(input_stream)
57
 
58
  token_stream = CommonTokenStream(lexer)
59
  parser = HPC16Parser(token_stream)
60
  parser._errHandler = BailErrorStrategy()
61
 
62
  try :
63
    tree = parser.prog()
64
    walker = ParseTreeWalker()
65
    walker.walk(MyHPC16Listener(fout), tree)
66
  except ParseCancellationException, e:
67
    print(sys.argv[0], "\nERROR Found invalid instruction\n in:", sys.argv[1], file=sys.stderr)
68
    fout.close()
69
  else:
70
    fout.close()
71
    mkrom_vhdl_sim(fout_name, a + "_init_ram.txt")
72
 
73
 
74
 
75
def mkrom_vhdl_sim(fin_name , fout_name):
76
  fin  = open(fin_name, 'r')
77
  fout = open(fout_name, 'w')
78
  lc = 0
79
  for line in fin:
80
    if not re.match("^#", line):
81
      b = "{:#018b}".format(int(line, 16))
82
      s = str(lc) + ":" + b[2:]
83
      print(s, file=fout)
84
      lc = lc + 1
85
 
86
  fin.close()
87
  fout.close()
88
 
89
 
90
def usage():
91
  print("usage:\n", sys.argv[0], "<input file>\n",  file=sys.stderr)
92
 
93
 
94
 
95
if __name__ == '__main__':
96
  start()
97
 
98
 
99
 
100
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.