OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [zasm/] [zparser.cpp] - Blame information for rev 42

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    zparser.cpp
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU core
6
//
7 13 dgisselq
// Purpose:     This file is really mis-named.  At one time it was going to
8
//              be the parser for the Zip Assembler, zasm.  Since then, I
9
//              discovered Flex and Bison and have written a parser using
10
//              those tools.  The true parser may therefore be found in zasm.y.
11
//              This file, however, still contains some very valuable tools.
12
//              In particular, all of the routines used to build instructions
13
//              from the appropriate fields are kept in this file.  For example,
14
//              op_noop() returns the instruction code for a NOOP  instruction.
15 2 dgisselq
//
16
// Creator:     Dan Gisselquist, Ph.D.
17
//              Gisselquist Tecnology, LLC
18
//
19
////////////////////////////////////////////////////////////////////////////////
20
//
21
// Copyright (C) 2015, Gisselquist Technology, LLC
22
//
23
// This program is free software (firmware): you can redistribute it and/or
24
// modify it under the terms of  the GNU General Public License as published
25
// by the Free Software Foundation, either version 3 of the License, or (at
26
// your option) any later version.
27
//
28
// This program is distributed in the hope that it will be useful, but WITHOUT
29
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
30
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31
// for more details.
32
//
33
// You should have received a copy of the GNU General Public License along
34
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
35
// target there if the PDF file isn't present.)  If not, see
36
// <http://www.gnu.org/licenses/> for a copy.
37
//
38
// License:     GPL, v3, as defined and found on www.gnu.org,
39
//              http://www.gnu.org/licenses/gpl.html
40
//
41
//
42
////////////////////////////////////////////////////////////////////////////////
43
 
44
#include <stdio.h>
45
#include <stdlib.h>
46
#include <string.h>
47
#include <ctype.h>
48
#include <strings.h>
49
 
50
#include "zparser.h"
51
#include "zopcodes.h"
52
 
53
typedef ZPARSER::ZIPI ZIPI;     // A Zip Instruction (i.e. uint32)
54
 
55
#define IMMOP(OP,CND,IMM,A) (((OP&0x0f)<<28)|((A&0x0f)<<24)|((CND&0x07)<<21) \
56
                        | (IMM & 0x0fffff))
57
 
58
#define DBLREGOP(OP,CND,IMM,B,A) (((OP&0x0f)<<28)|((A&0x0f)<<24)        \
59
                        |((CND&0x07)<<21)|(1<<20)|((B&0x0f)<<16)         \
60
                        | (IMM & 0x0ffff))
61
 
62
ZIPI    ZPARSER::op_cmp(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
63
        return DBLREGOP(0x0, cnd, imm, b, a);
64
}
65
 
66
ZIPI    ZPARSER::op_cmp(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
67
        return IMMOP(0x0, cnd, imm, a);
68
}
69
 
70
 
71
ZIPI    ZPARSER::op_tst(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
72
        return DBLREGOP(0x1, cnd, imm, b, a);
73
} ZIPI  ZPARSER::op_tst(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
74
        return IMMOP(0x1, cnd, imm, a);
75
}
76
 
77
ZIPI    ZPARSER::op_mov(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
78
        ZIPI    in;
79
        in = (0x02 << 28)|((a&0x0f)<<24)|((cnd&0x07)<<21);
80
        in |= (a&0x10)<<16;
81
        in |= (b&0x0f)<<16;
82
        in |= (b&0x10)<<11;
83
        in |= imm & 0x07fff;
84
        return in;
85
}
86
 
87
 
88
ZIPI    ZPARSER::op_ldi(ZIPIMM imm, ZIPREG a) const {
89
        ZIPI    in;
90
        in = ((0x03)<<28) | ((a&0x0f)<<24) | (imm & ((1<<24)-1));
91
        return in;
92
}
93
 
94
ZIPI    ZPARSER::op_trap(ZIPCOND cnd, ZIPIMM imm) const {
95
        ZIPI    in;
96 13 dgisselq
        if (cnd != ZIPC_ALWAYS)
97
                return op_ldilo(cnd, imm, ZIP_CC);
98
        else
99
                return op_ldi(imm, ZIP_CC);
100
        // in  = ((0x4f)<<24)|((cnd&0x07)<<21)|(1<<20)|((0x0e)<<16);
101
        // in |= (imm & 0x0ffff);
102 2 dgisselq
        return in;
103
}
104
 
105
ZIPI    ZPARSER::op_noop(void) const {
106
        return 0x4e000000;
107
}
108
ZIPI    ZPARSER::op_break(void) const {
109
        return 0x4e000001;
110
}
111
 
112
ZIPI    ZPARSER::op_ldihi(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
113
        ZIPI    in;
114
        in  = ((0x4f)<<24)|((cnd&0x07)<<21)|(1<<20)|((a&0x0f)<<16);
115
        in |= (imm & 0x0ffff);
116
        return in;
117
}
118
ZIPI    ZPARSER::op_ldilo(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
119
        ZIPI    in;
120
        in  = ((0x4f)<<24)|((cnd&0x07)<<21)|(0<<20)|((a&0x0f)<<16);
121
        in |= (imm & 0x0ffff);
122
        return in;
123
}
124
 
125 26 dgisselq
ZIPI    ZPARSER::op_mpyu(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
126
        return (0x04<<28)|((a&0x0f)<<24)|((cnd&0x7)<<21)|(0<<20)|((b&0x0f)<<16)|(imm & 0x0ffff);
127
} ZIPI  ZPARSER::op_mpyu(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
128
        return (0x04<<28)|((a&0x0f)<<24)|((cnd&0x7)<<21)|(0<<20)|((0x0f)<<16)|(imm & 0x0ffff);
129 8 dgisselq
}
130
 
131 26 dgisselq
ZIPI    ZPARSER::op_mpys(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
132
        return (0x04<<28)|((a&0x0f)<<24)|((cnd&0x7)<<21)|(1<<20)|((b&0x0f)<<16)|(imm & 0x0ffff);
133
} ZIPI  ZPARSER::op_mpys(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
134
        return (0x04<<28)|((a&0x0f)<<24)|((cnd&0x7)<<21)|(1<<20)|((0x0f)<<16)|(imm & 0x0ffff);
135
}
136
 
137 8 dgisselq
ZIPI    ZPARSER::op_rol(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
138
        return DBLREGOP(0x5, cnd, imm, b, a);
139
} ZIPI  ZPARSER::op_rol(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
140
        return IMMOP(0x5, cnd, imm, a);
141
}
142
 
143 2 dgisselq
ZIPI    ZPARSER::op_lod(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
144
        return DBLREGOP(0x6, cnd, imm, b, a);
145 8 dgisselq
} ZIPI  ZPARSER::op_lod(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
146 2 dgisselq
        return IMMOP(0x6, cnd, imm, a);
147
}
148
 
149
 
150
ZIPI    ZPARSER::op_sto(ZIPCOND cnd, ZIPREG v, ZIPIMM imm, ZIPREG b) const {
151
        return DBLREGOP(0x7, cnd, imm, b, v);
152
} ZIPI  ZPARSER::op_sto(ZIPCOND cnd, ZIPREG v, ZIPIMM imm) const {
153
        return IMMOP(0x7, cnd, imm, v);
154
}
155
 
156
 
157
ZIPI    ZPARSER::op_sub(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
158
        return DBLREGOP(0x8, cnd, imm, b, a);
159
} ZIPI  ZPARSER::op_sub(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
160 26 dgisselq
        // While it seems like we might do well replacing a subtract immediate
161
        // with an add of the negative same, the conditions aren't the same
162
        // when doing so.  Hence this is an invalid substitution.
163
        // return IMMOP(0xa, cnd, -imm, a); // Do an add of the negative of imm
164 2 dgisselq
        return IMMOP(0x8, cnd, imm, a);
165
}
166
 
167
 
168
ZIPI    ZPARSER::op_and(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
169
        return DBLREGOP(0x9, cnd, imm, b, a);
170
} ZIPI  ZPARSER::op_and(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
171
        return IMMOP(0x9, cnd, imm, a);
172
}
173
 
174
 
175
ZIPI    ZPARSER::op_add(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
176
        return DBLREGOP(0xa, cnd, imm, b, a);
177
} ZIPI  ZPARSER::op_add(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
178
        return IMMOP(0xa, cnd, imm, a);
179
}
180
 
181
 
182
ZIPI    ZPARSER::op_or(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
183
        return DBLREGOP(0xb, cnd, imm, b, a);
184
} ZIPI  ZPARSER::op_or(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
185
        return IMMOP(0xb, cnd, imm, a);
186
}
187
 
188
ZIPI    ZPARSER::op_xor(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
189
        return DBLREGOP(0xc, cnd, imm, b, a);
190
} ZIPI  ZPARSER::op_xor(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
191
        return IMMOP(0xc, cnd, imm, a);
192
}
193
 
194
ZIPI    ZPARSER::op_lsl(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
195
        return DBLREGOP(0xd, cnd, imm, b, a);
196
} ZIPI  ZPARSER::op_lsl(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
197
        return IMMOP(0xd, cnd, imm, a);
198
}
199
 
200
ZIPI    ZPARSER::op_asr(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
201
        return DBLREGOP(0xe, cnd, imm, b, a);
202
} ZIPI  ZPARSER::op_asr(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
203
        return IMMOP(0xe, cnd, imm, a);
204
}
205
 
206
ZIPI    ZPARSER::op_lsr(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
207
        return DBLREGOP(0xf, cnd, imm, b, a);
208
} ZIPI  ZPARSER::op_lsr(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
209
        return IMMOP(0xf, cnd, imm, a);
210
}
211
 

powered by: WebSVN 2.1.0

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