summaryrefslogtreecommitdiffstats
path: root/src/cassembler.h
blob: 893569a75122e3152c4e969539135862b0b7bba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <klistview.h>
#include <algorithm>
#include <cctype>

#include "types.h"

class CCode ;

using namespace std ;

enum instrNumber {
	ADD, ADDCY, AND, CALL, COMPARE, DISABLE, ENABLE, FETCH, INPUT,
	JUMP, LOAD, OR, OUTPUT, RETURN, RETURNI, ROTATE, RL, RR, SL0,
	SL1, SLA, SLX, SR0, SR1, SRA, SRX, STORE, SUB, SUBCY, TEST,
	XOR 
} ;

class CNamereg {
	public:
		CNamereg() {} ;
		~CNamereg() {} ;
	
		string reg ;
		string name ;
} ;

class CConstant {
	public:
		CConstant() {}
		~CConstant() {}
	
		string value ;
		string name ;
} ;

class CLabel {
	public:
		CLabel() {}
		~CLabel() ;
	
		string value ;
		string name ;
} ;

class CSourceLine {
	public:
		enum SymbolType {
			stNone,
			stLabel,
			stNamereg,
			stConstant,
			stAddress 
		} ;
	
		CSourceLine( unsigned int lineNr ) : m_lineNr( lineNr ) 
		{ 
			m_type = stNone ; 
		} 
		~CSourceLine() {} ;

		void addColumn( string word ) 
		{ 
/*			int i ;										// Case sensitive
			for ( i = 0 ; i < word.length(); i++ ) 
				word[ i ] = toupper( word[ i ] ) ;
*/			m_line.push_back( word ) ; 
		}
		
		bool isColumn( unsigned int index ) 
		{ 
			return m_line.size() > index ; 
		}
		
		string getColumn( int index ) 
		{ 
			if ( !isColumn( index ) ) 
				return "" ; 
			else 
				return m_line[index] ; 
		}
		
		unsigned int m_lineNr; 
		vector<string> m_line ;
		unsigned int m_address ;
		SymbolType m_type ;
} ;


class CAssembler {
	public:
		CAssembler() ;
		~CAssembler() ;

		void setCode( CCode *code ) 
		{ 
			m_code = code ; 
		}
		void setFilename( string filename ) 
		{ 
			m_filename = filename ; 
		}
		bool assemble() ;
		
		void clear() { 
			m_source.clear() ; 
			m_registerTable.clear() ; 
			m_labelTable.clear() ; 
			m_constantTable.clear() ; 
		}
		void setMessageList( KListView *messageList ) 
		{ 
			m_messageList = messageList ; 
		}

		bool exportVHDL( string templateFile, string outputDir, string entityName ) ;
		bool exportHEX( string filename, bool mem ) ;

	protected:
		list<CSourceLine*> m_source ;
		list<CNamereg*> m_registerTable ;
		list<CConstant*> m_constantTable ;
		list<CLabel*> m_labelTable ;
		string m_filename ;
		bool buildSymbolTable() ;
		bool loadFile() ;
		
		void error( unsigned int line, const char *description ) ;
		int getRegister( string name ) ;
		
		char * getWord( char *s, char *word ) ;
		CSourceLine * formatLine( int lineNr, char *s ) ;
		
		int getInstruction( string name ) ;
		bool createOpcodes() ;
		
		string translateLabel( string name ) ;
		string translateConstant( string name ) ;
		string translateRegister( string name ) ;
		bool addInstruction( instrNumber instr, CSourceLine sourceLine, int offset ) ;
		
		CCode * m_code ;
		KListView *m_messageList ;
} ;