| 1 |
2 |
jondawson |
#!/usr/bin/env python
|
| 2 |
|
|
"""Generate Verilog Implementation of Instructions
|
| 3 |
|
|
|
| 4 |
|
|
The area optimized implementation uses a CPU like architecture.
|
| 5 |
|
|
+ Instructions are implemented in block RAM.
|
| 6 |
|
|
+ Registers are implemented in dual port RAM.
|
| 7 |
|
|
+ Only one instruction can be executed at a time.
|
| 8 |
|
|
+ The CPU uses a pipeline architecture, and will take 2 clocks to execute a taken branch.
|
| 9 |
|
|
+ A minimal instruction set is determined at compile time, and only those instructions are implemented.
|
| 10 |
|
|
|
| 11 |
|
|
"""
|
| 12 |
|
|
|
| 13 |
|
|
__author__ = "Jon Dawson"
|
| 14 |
|
|
__copyright__ = "Copyright (C) 2013, Jonathan P Dawson"
|
| 15 |
|
|
__version__ = "0.1"
|
| 16 |
|
|
|
| 17 |
4 |
jondawson |
import fpu
|
| 18 |
|
|
|
| 19 |
2 |
jondawson |
def unique(l):
|
| 20 |
|
|
|
| 21 |
4 |
jondawson |
"""In the absence of set in older python implementations, make list values unique"""
|
| 22 |
2 |
jondawson |
|
| 23 |
4 |
jondawson |
return dict(zip(l, l)).keys()
|
| 24 |
2 |
jondawson |
|
| 25 |
|
|
def log2(instructions):
|
| 26 |
|
|
|
| 27 |
4 |
jondawson |
"""Integer only algorithm to calculate the number of bits needed to store a number"""
|
| 28 |
2 |
jondawson |
|
| 29 |
4 |
jondawson |
bits = 1
|
| 30 |
|
|
power = 2
|
| 31 |
|
|
while power < instructions:
|
| 32 |
|
|
bits += 1
|
| 33 |
|
|
power *= 2
|
| 34 |
|
|
return bits
|
| 35 |
2 |
jondawson |
|
| 36 |
|
|
def print_verilog_literal(size, value):
|
| 37 |
|
|
|
| 38 |
|
|
"""Print a verilog literal with expicilt size"""
|
| 39 |
|
|
|
| 40 |
|
|
if(value >= 0):
|
| 41 |
|
|
return "%s'd%s"%(size, value)
|
| 42 |
|
|
else:
|
| 43 |
|
|
return "-%s'd%s"%(size, abs(value))
|
| 44 |
|
|
|
| 45 |
|
|
def remove_register_hazards(instructions):
|
| 46 |
|
|
|
| 47 |
|
|
"""search through instructions, and remove register hazards"""
|
| 48 |
|
|
|
| 49 |
|
|
wait_2_for = None
|
| 50 |
|
|
wait_1_for = None
|
| 51 |
|
|
new_instructions = []
|
| 52 |
|
|
for instruction in instructions:
|
| 53 |
|
|
wait = 0
|
| 54 |
|
|
if "src" in instruction:
|
| 55 |
|
|
if instruction["src"] == wait_1_for:
|
| 56 |
|
|
wait = max(wait, 1)
|
| 57 |
|
|
if instruction["src"] == wait_2_for:
|
| 58 |
|
|
wait = max(wait, 2)
|
| 59 |
|
|
if "srcb" in instruction:
|
| 60 |
|
|
if instruction["srcb"] == wait_1_for:
|
| 61 |
|
|
wait = max(wait, 1)
|
| 62 |
|
|
if instruction["srcb"] == wait_2_for:
|
| 63 |
|
|
wait = max(wait, 2)
|
| 64 |
|
|
for i in range(wait):
|
| 65 |
4 |
jondawson |
new_instructions.append({"op":"nop"})
|
| 66 |
2 |
jondawson |
new_instructions.append(instruction)
|
| 67 |
|
|
|
| 68 |
|
|
if instruction["op"] != "label":
|
| 69 |
|
|
wait_1_for = wait_2_for
|
| 70 |
|
|
if "dest" in instruction:
|
| 71 |
|
|
wait_2_for = instruction["dest"]
|
| 72 |
|
|
else:
|
| 73 |
|
|
wait_2_for = None
|
| 74 |
|
|
return new_instructions
|
| 75 |
|
|
|
| 76 |
|
|
def generate_instruction_set(instructions):
|
| 77 |
|
|
|
| 78 |
|
|
"""Calculate the required instruction set"""
|
| 79 |
|
|
|
| 80 |
|
|
instruction_set = []
|
| 81 |
|
|
instruction_memory = []
|
| 82 |
|
|
for instruction in instructions:
|
| 83 |
|
|
opcode = {}
|
| 84 |
|
|
encoded_instruction = {}
|
| 85 |
|
|
encoded_instruction["dest"] = 0
|
| 86 |
|
|
encoded_instruction["src"] = 0
|
| 87 |
|
|
encoded_instruction["srcb"] = 0
|
| 88 |
|
|
encoded_instruction["literal"] = 0
|
| 89 |
4 |
jondawson |
encoded_instruction["float"] = True
|
| 90 |
2 |
jondawson |
opcode["op"] = instruction["op"]
|
| 91 |
|
|
opcode["right"] = False
|
| 92 |
|
|
opcode["unsigned"] = False
|
| 93 |
4 |
jondawson |
opcode["literal"] = False
|
| 94 |
|
|
opcode["float"] = False
|
| 95 |
2 |
jondawson |
|
| 96 |
|
|
if "signed" in instruction:
|
| 97 |
|
|
opcode["unsigned"] = not instruction["signed"]
|
| 98 |
|
|
|
| 99 |
|
|
if "element_size" in instruction:
|
| 100 |
|
|
opcode["element_size"] = instruction["element_size"]
|
| 101 |
|
|
|
| 102 |
4 |
jondawson |
if "file_name" in instruction:
|
| 103 |
|
|
opcode["file_name"] = instruction["file_name"]
|
| 104 |
|
|
|
| 105 |
2 |
jondawson |
if "file" in instruction:
|
| 106 |
4 |
jondawson |
opcode["file"] = instruction["file"]
|
| 107 |
2 |
jondawson |
|
| 108 |
|
|
if "line" in instruction:
|
| 109 |
|
|
opcode["line"] = instruction["line"]
|
| 110 |
|
|
|
| 111 |
|
|
if "input" in instruction:
|
| 112 |
|
|
opcode["input"] = instruction["input"]
|
| 113 |
|
|
|
| 114 |
|
|
if "output" in instruction:
|
| 115 |
|
|
opcode["output"] = instruction["output"]
|
| 116 |
|
|
|
| 117 |
|
|
if "dest" in instruction:
|
| 118 |
|
|
encoded_instruction["dest"] = instruction["dest"]
|
| 119 |
|
|
|
| 120 |
|
|
if "src" in instruction:
|
| 121 |
|
|
encoded_instruction["src"] = instruction["src"]
|
| 122 |
|
|
|
| 123 |
|
|
if "srcb" in instruction:
|
| 124 |
|
|
encoded_instruction["srcb"] = instruction["srcb"]
|
| 125 |
|
|
|
| 126 |
|
|
if "left" in instruction:
|
| 127 |
|
|
opcode["literal"] = True
|
| 128 |
|
|
encoded_instruction["literal"] = instruction["left"]
|
| 129 |
|
|
|
| 130 |
|
|
if "right" in instruction:
|
| 131 |
|
|
opcode["literal"] = True
|
| 132 |
|
|
opcode["right"] = True
|
| 133 |
|
|
encoded_instruction["literal"] = instruction["right"]
|
| 134 |
|
|
|
| 135 |
4 |
jondawson |
if "type" in instruction:
|
| 136 |
|
|
if instruction["type"] == "float":
|
| 137 |
|
|
opcode["float"] = True
|
| 138 |
|
|
encoded_instruction["float"] = True
|
| 139 |
|
|
|
| 140 |
2 |
jondawson |
if "literal" in instruction:
|
| 141 |
|
|
opcode["literal"] = True
|
| 142 |
|
|
encoded_instruction["literal"] = instruction["literal"]
|
| 143 |
|
|
|
| 144 |
|
|
if "label" in instruction:
|
| 145 |
|
|
opcode["literal"] = True
|
| 146 |
|
|
encoded_instruction["literal"] = instruction["label"]
|
| 147 |
|
|
|
| 148 |
|
|
if opcode not in instruction_set:
|
| 149 |
|
|
instruction_set.append(opcode)
|
| 150 |
|
|
|
| 151 |
|
|
for op, test_opcode in enumerate(instruction_set):
|
| 152 |
|
|
if test_opcode == opcode:
|
| 153 |
|
|
encoded_instruction["op"] = op
|
| 154 |
|
|
encoded_instruction["comment"] = repr(instruction)
|
| 155 |
|
|
instruction_memory.append(encoded_instruction)
|
| 156 |
|
|
break
|
| 157 |
|
|
|
| 158 |
|
|
return instruction_set, instruction_memory
|
| 159 |
|
|
|
| 160 |
|
|
def calculate_jumps(instructions):
|
| 161 |
|
|
|
| 162 |
4 |
jondawson |
"""change symbolic labels into numeric addresses"""
|
| 163 |
2 |
jondawson |
|
| 164 |
4 |
jondawson |
#calculate the values of jump locations
|
| 165 |
|
|
location = 0
|
| 166 |
|
|
labels = {}
|
| 167 |
|
|
new_instructions = []
|
| 168 |
|
|
for instruction in instructions:
|
| 169 |
|
|
if instruction["op"] == "label":
|
| 170 |
|
|
labels[instruction["label"]] = location
|
| 171 |
|
|
else:
|
| 172 |
|
|
new_instructions.append(instruction)
|
| 173 |
|
|
location += 1
|
| 174 |
|
|
instructions = new_instructions
|
| 175 |
2 |
jondawson |
|
| 176 |
4 |
jondawson |
#substitue real values for labeled jump locations
|
| 177 |
|
|
for instruction in instructions:
|
| 178 |
|
|
if "label" in instruction:
|
| 179 |
|
|
instruction["label"]=labels[instruction["label"]]
|
| 180 |
2 |
jondawson |
|
| 181 |
4 |
jondawson |
return instructions
|
| 182 |
2 |
jondawson |
|
| 183 |
|
|
def generate_declarations(instructions, no_tb_mode, register_bits, opcode_bits):
|
| 184 |
|
|
|
| 185 |
4 |
jondawson |
"""Generate verilog declarations"""
|
| 186 |
2 |
jondawson |
|
| 187 |
4 |
jondawson |
#list all inputs and outputs used in the program
|
| 188 |
|
|
inputs = unique([i["input"] for i in instructions if "input" in i])
|
| 189 |
|
|
outputs = unique([i["output"] for i in instructions if "output" in i])
|
| 190 |
|
|
input_files = unique([i["file_name"] for i in instructions if "file_read" == i["op"]])
|
| 191 |
|
|
output_files = unique([i["file_name"] for i in instructions if "file_write" == i["op"]])
|
| 192 |
|
|
testbench = not inputs and not outputs and not no_tb_mode
|
| 193 |
2 |
jondawson |
|
| 194 |
4 |
jondawson |
#Do not generate a port in testbench mode
|
| 195 |
|
|
inports = [
|
| 196 |
|
|
("input_" + i, 16) for i in inputs
|
| 197 |
|
|
] + [
|
| 198 |
|
|
("input_" + i + "_stb", 1) for i in inputs
|
| 199 |
|
|
] + [
|
| 200 |
|
|
("output_" + i + "_ack", 1) for i in outputs
|
| 201 |
|
|
]
|
| 202 |
2 |
jondawson |
|
| 203 |
4 |
jondawson |
outports = [
|
| 204 |
|
|
("output_" + i, 16) for i in outputs
|
| 205 |
|
|
] + [
|
| 206 |
|
|
("output_" + i + "_stb", 1) for i in outputs
|
| 207 |
|
|
] + [
|
| 208 |
|
|
("input_" + i + "_ack", 1) for i in inputs
|
| 209 |
|
|
]
|
| 210 |
2 |
jondawson |
|
| 211 |
4 |
jondawson |
#create list of signals
|
| 212 |
|
|
signals = [
|
| 213 |
|
|
("timer", 16),
|
| 214 |
|
|
("timer_enable", 1),
|
| 215 |
|
|
("stage_0_enable", 1),
|
| 216 |
|
|
("stage_1_enable", 1),
|
| 217 |
|
|
("stage_2_enable", 1),
|
| 218 |
|
|
("program_counter", log2(len(instructions))),
|
| 219 |
|
|
("program_counter_0", log2(len(instructions))),
|
| 220 |
|
|
("instruction_0", 32 + register_bits*2 + opcode_bits),
|
| 221 |
|
|
("opcode_0", opcode_bits),
|
| 222 |
|
|
("dest_0", register_bits),
|
| 223 |
|
|
("src_0", register_bits),
|
| 224 |
|
|
("srcb_0", register_bits),
|
| 225 |
|
|
("literal_0", 32),
|
| 226 |
|
|
("program_counter_1", log2(len(instructions))),
|
| 227 |
|
|
("opcode_1", opcode_bits),
|
| 228 |
|
|
("dest_1", register_bits),
|
| 229 |
|
|
("register_1", 32),
|
| 230 |
|
|
("registerb_1", 32),
|
| 231 |
|
|
("literal_1", 32),
|
| 232 |
|
|
("dest_2", register_bits),
|
| 233 |
|
|
("result_2", 32),
|
| 234 |
|
|
("write_enable_2", 1),
|
| 235 |
|
|
("address_2", 16),
|
| 236 |
|
|
("data_out_2", 16),
|
| 237 |
|
|
("data_in_2", 16),
|
| 238 |
|
|
("memory_enable_2", 1),
|
| 239 |
|
|
("address_4", 16),
|
| 240 |
|
|
("data_out_4", 32),
|
| 241 |
|
|
("data_in_4", 32),
|
| 242 |
|
|
("memory_enable_4", 1),
|
| 243 |
|
|
] + [
|
| 244 |
|
|
("s_output_" + i + "_stb", 16) for i in outputs
|
| 245 |
|
|
] + [
|
| 246 |
|
|
("s_output_" + i, 16) for i in outputs
|
| 247 |
|
|
] + [
|
| 248 |
|
|
("s_input_" + i + "_ack", 16) for i in inputs
|
| 249 |
|
|
]
|
| 250 |
2 |
jondawson |
|
| 251 |
4 |
jondawson |
if testbench:
|
| 252 |
|
|
signals.append(("clk", 1))
|
| 253 |
|
|
signals.append(("rst", 1))
|
| 254 |
|
|
else:
|
| 255 |
|
|
inports.append(("clk", 1))
|
| 256 |
|
|
inports.append(("rst", 1))
|
| 257 |
2 |
jondawson |
|
| 258 |
4 |
jondawson |
return inputs, outputs, input_files, output_files, testbench, inports, outports, signals
|
| 259 |
2 |
jondawson |
|
| 260 |
4 |
jondawson |
def floating_point_enables(instruction_set):
|
| 261 |
|
|
enable_adder = False
|
| 262 |
|
|
enable_multiplier = False
|
| 263 |
|
|
enable_divider = False
|
| 264 |
|
|
enable_int_to_float = False
|
| 265 |
|
|
enable_float_to_int = False
|
| 266 |
|
|
for i in instruction_set:
|
| 267 |
|
|
if i["op"] == "+" and i["float"]:
|
| 268 |
|
|
enable_adder = True
|
| 269 |
|
|
if i["op"] == "-" and i["float"]:
|
| 270 |
|
|
enable_adder = True
|
| 271 |
|
|
if i["op"] == "*" and i["float"]:
|
| 272 |
|
|
enable_multiplier = True
|
| 273 |
|
|
if i["op"] == "/" and i["float"]:
|
| 274 |
|
|
enable_divider = True
|
| 275 |
|
|
if i["op"] == "int_to_float":
|
| 276 |
|
|
enable_int_to_float = True
|
| 277 |
|
|
if i["op"] == "float_to_int":
|
| 278 |
|
|
enable_float_to_int = True
|
| 279 |
|
|
return (
|
| 280 |
|
|
enable_adder,
|
| 281 |
|
|
enable_multiplier,
|
| 282 |
|
|
enable_divider,
|
| 283 |
|
|
enable_int_to_float,
|
| 284 |
|
|
enable_float_to_int)
|
| 285 |
|
|
|
| 286 |
|
|
def generate_CHIP(input_file,
|
| 287 |
|
|
name,
|
| 288 |
|
|
instructions,
|
| 289 |
|
|
output_file,
|
| 290 |
|
|
registers,
|
| 291 |
|
|
memory_size_2,
|
| 292 |
|
|
memory_size_4,
|
| 293 |
2 |
jondawson |
initialize_memory,
|
| 294 |
4 |
jondawson |
memory_content_2,
|
| 295 |
|
|
memory_content_4,
|
| 296 |
2 |
jondawson |
no_tb_mode=False):
|
| 297 |
|
|
|
| 298 |
4 |
jondawson |
"""A big ugly function to crunch through all the instructions and generate the CHIP equivilent"""
|
| 299 |
2 |
jondawson |
|
| 300 |
4 |
jondawson |
instructions = remove_register_hazards(instructions)
|
| 301 |
|
|
instructions = calculate_jumps(instructions)
|
| 302 |
|
|
instruction_set, instruction_memory = generate_instruction_set(instructions)
|
| 303 |
|
|
register_bits = log2(len(registers));
|
| 304 |
|
|
opcode_bits = log2(len(instruction_set));
|
| 305 |
|
|
instruction_bits = 32 + register_bits*2 + opcode_bits
|
| 306 |
|
|
declarations = generate_declarations(instructions, no_tb_mode, register_bits, opcode_bits)
|
| 307 |
|
|
inputs, outputs, input_files, output_files, testbench, inports, outports, signals = declarations
|
| 308 |
|
|
enable_adder, enable_multiplier, enable_divider, enable_int_to_float, enable_float_to_int = floating_point_enables(instruction_set)
|
| 309 |
2 |
jondawson |
|
| 310 |
4 |
jondawson |
#output the code in verilog
|
| 311 |
|
|
output_file.write("//////////////////////////////////////////////////////////////////////////////\n")
|
| 312 |
|
|
output_file.write("//name : %s\n"%name)
|
| 313 |
|
|
for i in inputs:
|
| 314 |
|
|
output_file.write("//input : input_%s:16\n"%i)
|
| 315 |
|
|
for i in outputs:
|
| 316 |
|
|
output_file.write("//output : output_%s:16\n"%i)
|
| 317 |
|
|
output_file.write("//source_file : %s\n"%input_file)
|
| 318 |
|
|
output_file.write("///%s\n"%"".join(["=" for i in name]))
|
| 319 |
|
|
output_file.write("///\n")
|
| 320 |
|
|
output_file.write("///Created by C2CHIP\n\n")
|
| 321 |
2 |
jondawson |
|
| 322 |
4 |
jondawson |
if enable_adder:
|
| 323 |
|
|
output_file.write(fpu.adder)
|
| 324 |
|
|
if enable_divider:
|
| 325 |
|
|
output_file.write(fpu.divider)
|
| 326 |
|
|
if enable_multiplier:
|
| 327 |
|
|
output_file.write(fpu.multiplier)
|
| 328 |
|
|
if enable_int_to_float:
|
| 329 |
|
|
output_file.write(fpu.int_to_float)
|
| 330 |
|
|
if enable_float_to_int:
|
| 331 |
|
|
output_file.write(fpu.float_to_int)
|
| 332 |
2 |
jondawson |
|
| 333 |
|
|
|
| 334 |
4 |
jondawson |
output_file.write("//////////////////////////////////////////////////////////////////////////////\n")
|
| 335 |
|
|
output_file.write("// Register Allocation\n")
|
| 336 |
|
|
output_file.write("// ===================\n")
|
| 337 |
|
|
output_file.write("// %s %s %s \n"%("Register".center(20), "Name".center(20), "Size".center(20)))
|
| 338 |
|
|
for register, definition in registers.iteritems():
|
| 339 |
|
|
register_name, size = definition
|
| 340 |
|
|
output_file.write("// %s %s %s \n"%(str(register).center(20), register_name.center(20), str(size).center(20)))
|
| 341 |
2 |
jondawson |
|
| 342 |
|
|
|
| 343 |
4 |
jondawson |
output_file.write("module %s"%name)
|
| 344 |
2 |
jondawson |
|
| 345 |
4 |
jondawson |
all_ports = [name for name, size in inports + outports]
|
| 346 |
|
|
if all_ports:
|
| 347 |
|
|
output_file.write("(")
|
| 348 |
|
|
output_file.write(",".join(all_ports))
|
| 349 |
|
|
output_file.write(");\n")
|
| 350 |
|
|
else:
|
| 351 |
|
|
output_file.write(";\n")
|
| 352 |
2 |
jondawson |
|
| 353 |
4 |
jondawson |
output_file.write(" integer file_count;\n")
|
| 354 |
2 |
jondawson |
|
| 355 |
|
|
|
| 356 |
4 |
jondawson |
if enable_adder:
|
| 357 |
|
|
generate_adder_signals(output_file)
|
| 358 |
|
|
if enable_multiplier:
|
| 359 |
|
|
generate_multiplier_signals(output_file)
|
| 360 |
|
|
if enable_divider:
|
| 361 |
|
|
generate_divider_signals(output_file)
|
| 362 |
|
|
if enable_int_to_float:
|
| 363 |
|
|
generate_int_to_float_signals(output_file)
|
| 364 |
|
|
if enable_float_to_int:
|
| 365 |
|
|
generate_float_to_int_signals(output_file)
|
| 366 |
2 |
jondawson |
|
| 367 |
4 |
jondawson |
output_file.write(" real fp_value;\n")
|
| 368 |
|
|
if enable_adder or enable_multiplier or enable_divider or enable_int_to_float or enable_float_to_int:
|
| 369 |
|
|
output_file.write(" parameter wait_go = 2'd0,\n")
|
| 370 |
|
|
output_file.write(" write_a = 2'd1,\n")
|
| 371 |
|
|
output_file.write(" write_b = 2'd2,\n")
|
| 372 |
|
|
output_file.write(" read_z = 2'd3;\n")
|
| 373 |
|
|
|
| 374 |
2 |
jondawson |
|
| 375 |
4 |
jondawson |
input_files = dict(zip(input_files, ["input_file_%s"%i for i, j in enumerate(input_files)]))
|
| 376 |
|
|
for i in input_files.values():
|
| 377 |
|
|
output_file.write(" integer %s;\n"%i)
|
| 378 |
2 |
jondawson |
|
| 379 |
4 |
jondawson |
output_files = dict(zip(output_files, ["output_file_%s"%i for i, j in enumerate(output_files)]))
|
| 380 |
|
|
for i in output_files.values():
|
| 381 |
|
|
output_file.write(" integer %s;\n"%i)
|
| 382 |
2 |
jondawson |
|
| 383 |
|
|
|
| 384 |
4 |
jondawson |
def write_declaration(object_type, name, size, value=None):
|
| 385 |
|
|
if size == 1:
|
| 386 |
|
|
output_file.write(object_type)
|
| 387 |
|
|
output_file.write(name)
|
| 388 |
|
|
if value is not None:
|
| 389 |
|
|
output_file.write("= %s'd%s"%(size,value))
|
| 390 |
|
|
output_file.write(";\n")
|
| 391 |
|
|
else:
|
| 392 |
|
|
output_file.write(object_type)
|
| 393 |
|
|
output_file.write("[%i:0]"%(size-1))
|
| 394 |
|
|
output_file.write(" ")
|
| 395 |
|
|
output_file.write(name)
|
| 396 |
|
|
if value is not None:
|
| 397 |
|
|
output_file.write("= %s'd%s"%(size,value))
|
| 398 |
|
|
output_file.write(";\n")
|
| 399 |
2 |
jondawson |
|
| 400 |
4 |
jondawson |
for name, size in inports:
|
| 401 |
|
|
write_declaration(" input ", name, size)
|
| 402 |
2 |
jondawson |
|
| 403 |
4 |
jondawson |
for name, size in outports:
|
| 404 |
|
|
write_declaration(" output ", name, size)
|
| 405 |
2 |
jondawson |
|
| 406 |
4 |
jondawson |
for name, size in signals:
|
| 407 |
|
|
write_declaration(" reg ", name, size)
|
| 408 |
2 |
jondawson |
|
| 409 |
4 |
jondawson |
memory_size_2 = int(memory_size_2)
|
| 410 |
|
|
memory_size_4 = int(memory_size_4)
|
| 411 |
|
|
if memory_size_2:
|
| 412 |
|
|
output_file.write(" reg [15:0] memory_2 [%i:0];\n"%(memory_size_2-1))
|
| 413 |
|
|
if memory_size_4:
|
| 414 |
|
|
output_file.write(" reg [31:0] memory_4 [%i:0];\n"%(memory_size_4-1))
|
| 415 |
2 |
jondawson |
|
| 416 |
4 |
jondawson |
output_file.write(" reg [%s:0] instructions [%i:0];\n"%(instruction_bits-1, len(instructions)-1))
|
| 417 |
|
|
output_file.write(" reg [31:0] registers [%i:0];\n"%(len(registers)-1))
|
| 418 |
2 |
jondawson |
|
| 419 |
|
|
|
| 420 |
4 |
jondawson |
#generate clock and reset in testbench mode
|
| 421 |
|
|
if testbench:
|
| 422 |
2 |
jondawson |
|
| 423 |
4 |
jondawson |
output_file.write("\n //////////////////////////////////////////////////////////////////////////////\n")
|
| 424 |
|
|
output_file.write(" // CLOCK AND RESET GENERATION \n")
|
| 425 |
|
|
output_file.write(" // \n")
|
| 426 |
|
|
output_file.write(" // This file was generated in test bench mode. In this mode, the verilog \n")
|
| 427 |
|
|
output_file.write(" // output file can be executed directly within a verilog simulator. \n")
|
| 428 |
|
|
output_file.write(" // In test bench mode, a simulated clock and reset signal are generated within\n")
|
| 429 |
|
|
output_file.write(" // the output file. \n")
|
| 430 |
|
|
output_file.write(" // Verilog files generated in testbecnch mode are not suitable for synthesis, \n")
|
| 431 |
|
|
output_file.write(" // or for instantiation within a larger design.\n")
|
| 432 |
2 |
jondawson |
|
| 433 |
4 |
jondawson |
output_file.write(" \n initial\n")
|
| 434 |
|
|
output_file.write(" begin\n")
|
| 435 |
|
|
output_file.write(" rst <= 1'b1;\n")
|
| 436 |
|
|
output_file.write(" #50 rst <= 1'b0;\n")
|
| 437 |
|
|
output_file.write(" end\n\n")
|
| 438 |
2 |
jondawson |
|
| 439 |
4 |
jondawson |
output_file.write(" \n initial\n")
|
| 440 |
|
|
output_file.write(" begin\n")
|
| 441 |
|
|
output_file.write(" clk <= 1'b0;\n")
|
| 442 |
|
|
output_file.write(" while (1) begin\n")
|
| 443 |
|
|
output_file.write(" #5 clk <= ~clk;\n")
|
| 444 |
|
|
output_file.write(" end\n")
|
| 445 |
|
|
output_file.write(" end\n\n")
|
| 446 |
2 |
jondawson |
|
| 447 |
4 |
jondawson |
#Instance Floating Point Arithmetic
|
| 448 |
|
|
if enable_adder or enable_multiplier or enable_divider or enable_int_to_float or enable_float_to_int:
|
| 449 |
2 |
jondawson |
|
| 450 |
4 |
jondawson |
output_file.write("\n //////////////////////////////////////////////////////////////////////////////\n")
|
| 451 |
|
|
output_file.write(" // Floating Point Arithmetic \n")
|
| 452 |
|
|
output_file.write(" // \n")
|
| 453 |
|
|
output_file.write(" // Generate IEEE 754 single precision divider, adder and multiplier \n")
|
| 454 |
|
|
output_file.write(" // \n")
|
| 455 |
2 |
jondawson |
|
| 456 |
4 |
jondawson |
if enable_divider:
|
| 457 |
|
|
connect_divider(output_file)
|
| 458 |
|
|
if enable_multiplier:
|
| 459 |
|
|
connect_multiplier(output_file)
|
| 460 |
|
|
if enable_adder:
|
| 461 |
|
|
connect_adder(output_file)
|
| 462 |
|
|
if enable_int_to_float:
|
| 463 |
|
|
connect_int_to_float(output_file)
|
| 464 |
|
|
if enable_float_to_int:
|
| 465 |
|
|
connect_float_to_int(output_file)
|
| 466 |
2 |
jondawson |
|
| 467 |
|
|
|
| 468 |
|
|
|
| 469 |
4 |
jondawson |
#Generate a state machine to execute the instructions
|
| 470 |
|
|
binary_operators = ["+", "-", "*", "/", "|", "&", "^", "<<", ">>", "<",">", ">=",
|
| 471 |
|
|
"<=", "==", "!="]
|
| 472 |
2 |
jondawson |
|
| 473 |
|
|
|
| 474 |
4 |
jondawson |
if initialize_memory and (memory_content_2 or memory_content_4):
|
| 475 |
2 |
jondawson |
|
| 476 |
4 |
jondawson |
output_file.write("\n //////////////////////////////////////////////////////////////////////////////\n")
|
| 477 |
|
|
output_file.write(" // MEMORY INITIALIZATION \n")
|
| 478 |
|
|
output_file.write(" // \n")
|
| 479 |
|
|
output_file.write(" // In order to reduce program size, array contents have been stored into \n")
|
| 480 |
|
|
output_file.write(" // memory at initialization. In an FPGA, this will result in the memory being \n")
|
| 481 |
|
|
output_file.write(" // initialized when the FPGA configures. \n")
|
| 482 |
|
|
output_file.write(" // Memory will not be re-initialized at reset. \n")
|
| 483 |
|
|
output_file.write(" // Dissable this behaviour using the no_initialize_memory switch \n")
|
| 484 |
2 |
jondawson |
|
| 485 |
4 |
jondawson |
output_file.write(" \n initial\n")
|
| 486 |
|
|
output_file.write(" begin\n")
|
| 487 |
|
|
for location, content in memory_content_2.iteritems():
|
| 488 |
|
|
output_file.write(" memory_2[%s] = %s;\n"%(location, content))
|
| 489 |
|
|
for location, content in memory_content_4.iteritems():
|
| 490 |
|
|
output_file.write(" memory_4[%s] = %s;\n"%(location, content))
|
| 491 |
|
|
output_file.write(" end\n\n")
|
| 492 |
2 |
jondawson |
|
| 493 |
|
|
|
| 494 |
4 |
jondawson |
output_file.write("\n //////////////////////////////////////////////////////////////////////////////\n")
|
| 495 |
|
|
output_file.write(" // INSTRUCTION INITIALIZATION \n")
|
| 496 |
|
|
output_file.write(" // \n")
|
| 497 |
|
|
output_file.write(" // Initialise the contents of the instruction memory \n")
|
| 498 |
|
|
output_file.write(" //\n")
|
| 499 |
|
|
output_file.write(" // Intruction Set\n")
|
| 500 |
|
|
output_file.write(" // ==============\n")
|
| 501 |
|
|
for num, opcode in enumerate(instruction_set):
|
| 502 |
|
|
output_file.write(" // %s %s\n"%(num, opcode))
|
| 503 |
2 |
jondawson |
|
| 504 |
4 |
jondawson |
output_file.write(" // Intructions\n")
|
| 505 |
|
|
output_file.write(" // ===========\n")
|
| 506 |
|
|
output_file.write(" \n initial\n")
|
| 507 |
|
|
output_file.write(" begin\n")
|
| 508 |
|
|
for location, instruction in enumerate(instruction_memory):
|
| 509 |
|
|
output_file.write(" instructions[%s] = {%s, %s, %s, %s};//%s\n"%(
|
| 510 |
|
|
location,
|
| 511 |
|
|
print_verilog_literal(opcode_bits, instruction["op"]),
|
| 512 |
|
|
print_verilog_literal(register_bits, instruction["dest"]),
|
| 513 |
|
|
print_verilog_literal(register_bits, instruction["src"]),
|
| 514 |
|
|
print_verilog_literal(32, instruction["srcb"] | instruction["literal"]),
|
| 515 |
|
|
instruction["comment"]))
|
| 516 |
|
|
output_file.write(" end\n\n")
|
| 517 |
2 |
jondawson |
|
| 518 |
4 |
jondawson |
if input_files or output_files:
|
| 519 |
2 |
jondawson |
|
| 520 |
4 |
jondawson |
output_file.write("\n //////////////////////////////////////////////////////////////////////////////\n")
|
| 521 |
|
|
output_file.write(" // OPEN FILES \n")
|
| 522 |
|
|
output_file.write(" // \n")
|
| 523 |
|
|
output_file.write(" // Open all files used at the start of the process \n")
|
| 524 |
2 |
jondawson |
|
| 525 |
4 |
jondawson |
output_file.write(" \n initial\n")
|
| 526 |
|
|
output_file.write(" begin\n")
|
| 527 |
|
|
for file_name, file_ in input_files.iteritems():
|
| 528 |
|
|
output_file.write(" %s = $fopenr(\"%s\");\n"%(file_, file_name))
|
| 529 |
|
|
for file_name, file_ in output_files.iteritems():
|
| 530 |
|
|
output_file.write(" %s = $fopen(\"%s\");\n"%(file_, file_name))
|
| 531 |
|
|
output_file.write(" end\n\n")
|
| 532 |
2 |
jondawson |
|
| 533 |
4 |
jondawson |
output_file.write("\n //////////////////////////////////////////////////////////////////////////////\n")
|
| 534 |
|
|
output_file.write(" // CPU IMPLEMENTAION OF C PROCESS \n")
|
| 535 |
|
|
output_file.write(" // \n")
|
| 536 |
|
|
output_file.write(" // This section of the file contains a CPU implementing the C process. \n")
|
| 537 |
2 |
jondawson |
|
| 538 |
4 |
jondawson |
output_file.write(" \n always @(posedge clk)\n")
|
| 539 |
|
|
output_file.write(" begin\n\n")
|
| 540 |
2 |
jondawson |
|
| 541 |
4 |
jondawson |
if memory_size_2:
|
| 542 |
|
|
output_file.write(" //implement memory for 2 byte x n arrays\n")
|
| 543 |
|
|
output_file.write(" if (memory_enable_2 == 1'b1) begin\n")
|
| 544 |
|
|
output_file.write(" memory_2[address_2] <= data_in_2;\n")
|
| 545 |
|
|
output_file.write(" end\n")
|
| 546 |
|
|
output_file.write(" data_out_2 <= memory_2[address_2];\n")
|
| 547 |
|
|
output_file.write(" memory_enable_2 <= 1'b0;\n\n")
|
| 548 |
|
|
|
| 549 |
|
|
if memory_size_4:
|
| 550 |
|
|
output_file.write(" //implement memory for 4 byte x n arrays\n")
|
| 551 |
|
|
output_file.write(" if (memory_enable_4 == 1'b1) begin\n")
|
| 552 |
|
|
output_file.write(" memory_4[address_4] <= data_in_4;\n")
|
| 553 |
|
|
output_file.write(" end\n")
|
| 554 |
|
|
output_file.write(" data_out_4 <= memory_4[address_4];\n")
|
| 555 |
|
|
output_file.write(" memory_enable_4 <= 1'b0;\n\n")
|
| 556 |
|
|
|
| 557 |
|
|
output_file.write(" write_enable_2 <= 0;\n")
|
| 558 |
|
|
|
| 559 |
|
|
if enable_divider:
|
| 560 |
|
|
output_file.write(" divider_go <= 0;\n")
|
| 561 |
|
|
if enable_multiplier:
|
| 562 |
|
|
output_file.write(" multiplier_go <= 0;\n")
|
| 563 |
|
|
if enable_adder:
|
| 564 |
|
|
output_file.write(" adder_go <= 0;\n")
|
| 565 |
|
|
if enable_int_to_float:
|
| 566 |
|
|
output_file.write(" int_to_float_go <= 0;\n")
|
| 567 |
|
|
if enable_float_to_int:
|
| 568 |
|
|
output_file.write(" float_to_int_go <= 0;\n")
|
| 569 |
|
|
|
| 570 |
|
|
output_file.write(" //stage 0 instruction fetch\n")
|
| 571 |
|
|
output_file.write(" if (stage_0_enable) begin\n")
|
| 572 |
|
|
output_file.write(" stage_1_enable <= 1;\n")
|
| 573 |
|
|
output_file.write(" instruction_0 <= instructions[program_counter];\n")
|
| 574 |
|
|
output_file.write(" opcode_0 = instruction_0[%s:%s];\n"%(
|
| 575 |
|
|
register_bits * 2 + opcode_bits + 31,
|
| 576 |
|
|
register_bits * 2 + 32))
|
| 577 |
|
|
output_file.write(" dest_0 = instruction_0[%s:%s];\n"%(
|
| 578 |
|
|
register_bits * 2 + 31,
|
| 579 |
|
|
register_bits + 32))
|
| 580 |
|
|
output_file.write(" src_0 = instruction_0[%s:32];\n"%(
|
| 581 |
|
|
register_bits + 31))
|
| 582 |
|
|
output_file.write(" srcb_0 = instruction_0[%s:0];\n"%(register_bits-1))
|
| 583 |
|
|
output_file.write(" literal_0 = instruction_0[31:0];\n")
|
| 584 |
|
|
output_file.write(" if(write_enable_2) begin\n")
|
| 585 |
|
|
output_file.write(" registers[dest_2] <= result_2;\n")
|
| 586 |
|
|
output_file.write(" end\n")
|
| 587 |
|
|
output_file.write(" program_counter_0 <= program_counter;\n")
|
| 588 |
|
|
output_file.write(" program_counter <= program_counter + 1;\n")
|
| 589 |
|
|
output_file.write(" end\n\n")
|
| 590 |
|
|
|
| 591 |
|
|
output_file.write(" //stage 1 opcode fetch\n")
|
| 592 |
|
|
output_file.write(" if (stage_1_enable) begin\n")
|
| 593 |
|
|
output_file.write(" stage_2_enable <= 1;\n")
|
| 594 |
|
|
output_file.write(" register_1 <= registers[src_0];\n")
|
| 595 |
|
|
output_file.write(" registerb_1 <= registers[srcb_0];\n")
|
| 596 |
|
|
output_file.write(" dest_1 <= dest_0;\n")
|
| 597 |
|
|
output_file.write(" literal_1 <= literal_0;\n")
|
| 598 |
|
|
output_file.write(" opcode_1 <= opcode_0;\n")
|
| 599 |
|
|
output_file.write(" program_counter_1 <= program_counter_0;\n")
|
| 600 |
|
|
output_file.write(" end\n\n")
|
| 601 |
|
|
|
| 602 |
|
|
output_file.write(" //stage 2 opcode fetch\n")
|
| 603 |
|
|
output_file.write(" if (stage_2_enable) begin\n")
|
| 604 |
|
|
output_file.write(" dest_2 <= dest_1;\n")
|
| 605 |
|
|
output_file.write(" case(opcode_1)\n\n")
|
| 606 |
|
|
|
| 607 |
|
|
#A frame is executed in each state
|
| 608 |
|
|
for opcode, instruction in enumerate(instruction_set):
|
| 609 |
|
|
|
| 610 |
|
|
if instruction["op"] == "literal":
|
| 611 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 612 |
|
|
output_file.write(" begin\n")
|
| 613 |
|
|
output_file.write(" result_2 <= literal_1;\n")
|
| 614 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 615 |
|
|
output_file.write(" end\n\n")
|
| 616 |
|
|
|
| 617 |
|
|
elif instruction["op"] == "move":
|
| 618 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 619 |
|
|
output_file.write(" begin\n")
|
| 620 |
|
|
output_file.write(" result_2 <= register_1;\n")
|
| 621 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 622 |
|
|
output_file.write(" end\n\n")
|
| 623 |
|
|
|
| 624 |
|
|
elif instruction["op"] == "~":
|
| 625 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 626 |
|
|
output_file.write(" begin\n")
|
| 627 |
|
|
output_file.write(" result_2 <= ~register_1;\n")
|
| 628 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 629 |
|
|
output_file.write(" end\n\n")
|
| 630 |
|
|
|
| 631 |
|
|
elif instruction["op"] == "int_to_float":
|
| 632 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 633 |
|
|
output_file.write(" begin\n")
|
| 634 |
|
|
output_file.write(" int_to <= register_1;\n")
|
| 635 |
|
|
output_file.write(" int_to_float_go <= 1;\n")
|
| 636 |
|
|
output_file.write(" stage_0_enable <= 0;\n")
|
| 637 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 638 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 639 |
|
|
output_file.write(" end\n\n")
|
| 640 |
|
|
|
| 641 |
|
|
elif instruction["op"] == "float_to_int":
|
| 642 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 643 |
|
|
output_file.write(" begin\n")
|
| 644 |
|
|
output_file.write(" float_to <= register_1;\n")
|
| 645 |
|
|
output_file.write(" float_to_int_go <= 1;\n")
|
| 646 |
|
|
output_file.write(" stage_0_enable <= 0;\n")
|
| 647 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 648 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 649 |
|
|
output_file.write(" end\n\n")
|
| 650 |
|
|
|
| 651 |
|
|
elif instruction["op"] in binary_operators:
|
| 652 |
2 |
jondawson |
if instruction["literal"]:
|
| 653 |
4 |
jondawson |
if instruction["float"]:
|
| 654 |
2 |
jondawson |
output_file.write(" 16'd%s:\n"%(opcode))
|
| 655 |
|
|
output_file.write(" begin\n")
|
| 656 |
4 |
jondawson |
if instruction["op"] == "/":
|
| 657 |
|
|
if instruction["right"]:
|
| 658 |
|
|
output_file.write(" divider_a <= register_1;\n")
|
| 659 |
|
|
output_file.write(" divider_b <= literal_1;\n")
|
| 660 |
|
|
else:
|
| 661 |
|
|
output_file.write(" divider_a <= literal_1;\n")
|
| 662 |
|
|
output_file.write(" divider_b <= register_1;\n")
|
| 663 |
|
|
output_file.write(" divider_go <= 1;\n")
|
| 664 |
|
|
elif instruction["op"] == "*":
|
| 665 |
|
|
if instruction["right"]:
|
| 666 |
|
|
output_file.write(" multiplier_a <= register_1;\n")
|
| 667 |
|
|
output_file.write(" multiplier_b <= literal_1;\n")
|
| 668 |
|
|
else:
|
| 669 |
|
|
output_file.write(" multiplier_a <= literal_1;\n")
|
| 670 |
|
|
output_file.write(" multiplier_b <= register_1;\n")
|
| 671 |
|
|
output_file.write(" multiplier_go <= 1;\n")
|
| 672 |
|
|
elif instruction["op"] == "+":
|
| 673 |
|
|
if instruction["right"]:
|
| 674 |
|
|
output_file.write(" adder_a <= register_1;\n")
|
| 675 |
|
|
output_file.write(" adder_b <= literal_1;\n")
|
| 676 |
|
|
else:
|
| 677 |
|
|
output_file.write(" adder_a <= literal_1;\n")
|
| 678 |
|
|
output_file.write(" adder_b <= register_1;\n")
|
| 679 |
|
|
output_file.write(" adder_go <= 1;\n")
|
| 680 |
|
|
elif instruction["op"] == "-":
|
| 681 |
|
|
if instruction["right"]:
|
| 682 |
|
|
output_file.write(" adder_a <= register_1;\n")
|
| 683 |
|
|
output_file.write(" adder_b <= {~literal_1[31], literal_1[30:0]};\n")
|
| 684 |
|
|
else:
|
| 685 |
|
|
output_file.write(" adder_a <= literal_1;\n")
|
| 686 |
|
|
output_file.write(" adder_b <= {~register_1[31], register_1[30:0]};\n")
|
| 687 |
|
|
output_file.write(" adder_go <= 1;\n")
|
| 688 |
|
|
output_file.write(" stage_0_enable <= 0;\n")
|
| 689 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 690 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 691 |
|
|
output_file.write(" end\n\n")
|
| 692 |
|
|
elif instruction["unsigned"]:
|
| 693 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 694 |
|
|
output_file.write(" begin\n")
|
| 695 |
2 |
jondawson |
if instruction["right"]:
|
| 696 |
|
|
output_file.write(" result_2 <= $unsigned(register_1) %s $unsigned(literal_1);\n"%(instruction["op"]))
|
| 697 |
|
|
else:
|
| 698 |
|
|
output_file.write(" result_2 <= $unsigned(literal_1) %s $unsigned(register_1);\n"%(instruction["op"]))
|
| 699 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 700 |
|
|
output_file.write(" end\n\n")
|
| 701 |
|
|
else:
|
| 702 |
|
|
if instruction["op"] == ">>":
|
| 703 |
|
|
instruction["op"] = ">>>"
|
| 704 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 705 |
|
|
output_file.write(" begin\n")
|
| 706 |
|
|
if instruction["right"]:
|
| 707 |
|
|
output_file.write(" result_2 <= $signed(register_1) %s $signed(literal_1);\n"%(instruction["op"]))
|
| 708 |
|
|
else:
|
| 709 |
|
|
output_file.write(" result_2 <= $signed(literal_1) %s $signed(register_1);\n"%(instruction["op"]))
|
| 710 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 711 |
|
|
output_file.write(" end\n\n")
|
| 712 |
|
|
else:
|
| 713 |
4 |
jondawson |
if instruction["float"]:
|
| 714 |
2 |
jondawson |
output_file.write(" 16'd%s:\n"%(opcode))
|
| 715 |
|
|
output_file.write(" begin\n")
|
| 716 |
4 |
jondawson |
if instruction["op"] == "/":
|
| 717 |
|
|
output_file.write(" divider_a <= register_1;\n")
|
| 718 |
|
|
output_file.write(" divider_b <= registerb_1;\n")
|
| 719 |
|
|
output_file.write(" divider_go <= 1;\n")
|
| 720 |
|
|
elif instruction["op"] == "*":
|
| 721 |
|
|
output_file.write(" multiplier_a <= register_1;\n")
|
| 722 |
|
|
output_file.write(" multiplier_b <= registerb_1;\n")
|
| 723 |
|
|
output_file.write(" multiplier_go <= 1;\n")
|
| 724 |
|
|
elif instruction["op"] == "+":
|
| 725 |
|
|
output_file.write(" adder_a <= register_1;\n")
|
| 726 |
|
|
output_file.write(" adder_b <= registerb_1;\n")
|
| 727 |
|
|
output_file.write(" adder_go <= 1;\n")
|
| 728 |
|
|
elif instruction["op"] == "-":
|
| 729 |
|
|
output_file.write(" adder_a <= register_1;\n")
|
| 730 |
|
|
output_file.write(" adder_b <= {~registerb_1[31], registerb_1[30:0]};\n")
|
| 731 |
|
|
output_file.write(" adder_go <= 1;\n")
|
| 732 |
|
|
output_file.write(" stage_0_enable <= 0;\n")
|
| 733 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 734 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 735 |
|
|
output_file.write(" end\n\n")
|
| 736 |
|
|
elif instruction["unsigned"]:
|
| 737 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 738 |
|
|
output_file.write(" begin\n")
|
| 739 |
2 |
jondawson |
output_file.write(" result_2 <= $unsigned(register_1) %s $unsigned(registerb_1);\n"%(instruction["op"]))
|
| 740 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 741 |
|
|
output_file.write(" end\n\n")
|
| 742 |
|
|
else:
|
| 743 |
|
|
if instruction["op"] == ">>":
|
| 744 |
|
|
instruction["op"] = ">>>"
|
| 745 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 746 |
|
|
output_file.write(" begin\n")
|
| 747 |
|
|
output_file.write(" result_2 <= $signed(register_1) %s $signed(registerb_1);\n"%(instruction["op"]))
|
| 748 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 749 |
|
|
output_file.write(" end\n\n")
|
| 750 |
|
|
|
| 751 |
4 |
jondawson |
elif instruction["op"] == "jmp_if_false":
|
| 752 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 753 |
|
|
output_file.write(" begin\n")
|
| 754 |
|
|
output_file.write(" if (register_1 == 0) begin\n");
|
| 755 |
|
|
output_file.write(" program_counter <= literal_1;\n")
|
| 756 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 757 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 758 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 759 |
|
|
output_file.write(" end\n")
|
| 760 |
|
|
output_file.write(" end\n\n")
|
| 761 |
2 |
jondawson |
|
| 762 |
4 |
jondawson |
elif instruction["op"] == "jmp_if_true":
|
| 763 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 764 |
|
|
output_file.write(" begin\n")
|
| 765 |
|
|
output_file.write(" if (register_1 != 0) begin\n");
|
| 766 |
|
|
output_file.write(" program_counter <= literal_1;\n")
|
| 767 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 768 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 769 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 770 |
|
|
output_file.write(" end\n")
|
| 771 |
|
|
output_file.write(" end\n\n")
|
| 772 |
2 |
jondawson |
|
| 773 |
4 |
jondawson |
elif instruction["op"] == "jmp_and_link":
|
| 774 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 775 |
|
|
output_file.write(" begin\n")
|
| 776 |
|
|
output_file.write(" program_counter <= literal_1;\n")
|
| 777 |
|
|
output_file.write(" result_2 <= program_counter_1 + 1;\n")
|
| 778 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 779 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 780 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 781 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 782 |
|
|
output_file.write(" end\n\n")
|
| 783 |
2 |
jondawson |
|
| 784 |
4 |
jondawson |
elif instruction["op"] == "jmp_to_reg":
|
| 785 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 786 |
|
|
output_file.write(" begin\n")
|
| 787 |
|
|
output_file.write(" program_counter <= register_1;\n")
|
| 788 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 789 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 790 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 791 |
|
|
output_file.write(" end\n\n")
|
| 792 |
2 |
jondawson |
|
| 793 |
4 |
jondawson |
elif instruction["op"] == "goto":
|
| 794 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 795 |
|
|
output_file.write(" begin\n")
|
| 796 |
|
|
output_file.write(" program_counter <= literal_1;\n")
|
| 797 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 798 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 799 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 800 |
|
|
output_file.write(" end\n\n")
|
| 801 |
2 |
jondawson |
|
| 802 |
4 |
jondawson |
elif instruction["op"] == "file_read":
|
| 803 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 804 |
|
|
output_file.write(" begin\n")
|
| 805 |
|
|
output_file.write(" file_count = $fscanf(%s, \"%%d\\n\", result_2);\n"%(
|
| 806 |
|
|
input_files[instruction["file_name"]]))
|
| 807 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 808 |
|
|
output_file.write(" end\n\n")
|
| 809 |
2 |
jondawson |
|
| 810 |
4 |
jondawson |
elif instruction["op"] == "file_write":
|
| 811 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 812 |
|
|
output_file.write(" begin\n")
|
| 813 |
|
|
if instruction["float"]:
|
| 814 |
|
|
output_file.write(' fp_value = (register_1[31]?-1.0:1.0) *\n')
|
| 815 |
|
|
output_file.write(' (2.0 ** (register_1[30:23]-127.0)) *\n')
|
| 816 |
|
|
output_file.write(' ({1\'d1, register_1[22:0]} / (2.0**23));\n')
|
| 817 |
|
|
output_file.write(' $fdisplay (%s, "%%f", fp_value);\n'%(
|
| 818 |
|
|
output_files[instruction["file_name"]]))
|
| 819 |
|
|
else:
|
| 820 |
|
|
output_file.write(" $fdisplay(%s, \"%%d\", register_1);\n"%(
|
| 821 |
|
|
output_files[instruction["file_name"]]))
|
| 822 |
|
|
output_file.write(" end\n\n")
|
| 823 |
2 |
jondawson |
|
| 824 |
4 |
jondawson |
elif instruction["op"] == "read":
|
| 825 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 826 |
|
|
output_file.write(" begin\n")
|
| 827 |
|
|
output_file.write(" stage_0_enable <= 0;\n")
|
| 828 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 829 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 830 |
|
|
output_file.write(" s_input_%s_ack <= 1'b1;\n"%instruction["input"])
|
| 831 |
|
|
output_file.write(" end\n\n")
|
| 832 |
2 |
jondawson |
|
| 833 |
4 |
jondawson |
elif instruction["op"] == "ready":
|
| 834 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 835 |
|
|
output_file.write(" begin\n")
|
| 836 |
|
|
output_file.write(" result_2 <= 0;\n")
|
| 837 |
|
|
output_file.write(" result_2[0] <= input_%s_stb;\n"%(
|
| 838 |
|
|
instruction["input"]))
|
| 839 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 840 |
|
|
output_file.write(" end\n\n")
|
| 841 |
2 |
jondawson |
|
| 842 |
4 |
jondawson |
elif instruction["op"] == "write":
|
| 843 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 844 |
|
|
output_file.write(" begin\n")
|
| 845 |
|
|
output_file.write(" stage_0_enable <= 0;\n")
|
| 846 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 847 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 848 |
|
|
output_file.write(" s_output_%s_stb <= 1'b1;\n"%instruction["output"])
|
| 849 |
|
|
output_file.write(" s_output_%s <= register_1;\n"%instruction["output"])
|
| 850 |
|
|
output_file.write(" end\n\n")
|
| 851 |
2 |
jondawson |
|
| 852 |
4 |
jondawson |
elif instruction["op"] == "memory_read_request":
|
| 853 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 854 |
|
|
output_file.write(" begin\n")
|
| 855 |
|
|
output_file.write(" address_%s <= register_1;\n"%(
|
| 856 |
|
|
instruction["element_size"]))
|
| 857 |
|
|
output_file.write(" end\n\n")
|
| 858 |
2 |
jondawson |
|
| 859 |
4 |
jondawson |
elif instruction["op"] == "memory_read_wait":
|
| 860 |
|
|
pass
|
| 861 |
2 |
jondawson |
|
| 862 |
4 |
jondawson |
elif instruction["op"] == "memory_read":
|
| 863 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 864 |
|
|
output_file.write(" begin\n")
|
| 865 |
|
|
output_file.write(" result_2 <= data_out_%s;\n"%(
|
| 866 |
|
|
instruction["element_size"]))
|
| 867 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 868 |
|
|
output_file.write(" end\n\n")
|
| 869 |
2 |
jondawson |
|
| 870 |
4 |
jondawson |
elif instruction["op"] == "memory_write":
|
| 871 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 872 |
|
|
output_file.write(" begin\n")
|
| 873 |
|
|
output_file.write(" address_%s <= register_1;\n"%(
|
| 874 |
|
|
instruction["element_size"]))
|
| 875 |
|
|
output_file.write(" data_in_%s <= registerb_1;\n"%(
|
| 876 |
|
|
instruction["element_size"]))
|
| 877 |
|
|
output_file.write(" memory_enable_%s <= 1'b1;\n"%(
|
| 878 |
|
|
instruction["element_size"]))
|
| 879 |
|
|
output_file.write(" end\n\n")
|
| 880 |
2 |
jondawson |
|
| 881 |
4 |
jondawson |
elif instruction["op"] == "assert":
|
| 882 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 883 |
|
|
output_file.write(" begin\n")
|
| 884 |
|
|
output_file.write(" if (register_1 == 0) begin\n")
|
| 885 |
|
|
output_file.write(" $display(\"Assertion failed at line: %s in file: %s\");\n"%(
|
| 886 |
|
|
instruction["line"],
|
| 887 |
|
|
instruction["file"]))
|
| 888 |
|
|
output_file.write(" $finish_and_return(1);\n")
|
| 889 |
|
|
output_file.write(" end\n")
|
| 890 |
|
|
output_file.write(" end\n\n")
|
| 891 |
2 |
jondawson |
|
| 892 |
4 |
jondawson |
elif instruction["op"] == "wait_clocks":
|
| 893 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 894 |
|
|
output_file.write(" begin\n")
|
| 895 |
|
|
output_file.write(" timer <= register_1;\n")
|
| 896 |
|
|
output_file.write(" timer_enable <= 1;\n")
|
| 897 |
|
|
output_file.write(" stage_0_enable <= 0;\n")
|
| 898 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 899 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 900 |
|
|
output_file.write(" end\n\n")
|
| 901 |
2 |
jondawson |
|
| 902 |
4 |
jondawson |
elif instruction["op"] == "report":
|
| 903 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 904 |
|
|
output_file.write(" begin\n")
|
| 905 |
|
|
if instruction["float"]:
|
| 906 |
|
|
|
| 907 |
|
|
output_file.write(' fp_value = (register_1[31]?-1.0:1.0) *\n')
|
| 908 |
|
|
output_file.write(' (2.0 ** (register_1[30:23]-127.0)) *\n')
|
| 909 |
|
|
output_file.write(' ({1\'d1, register_1[22:0]} / (2.0**23));\n')
|
| 910 |
2 |
jondawson |
|
| 911 |
4 |
jondawson |
output_file.write(' $display ("%%f (report at line: %s in file: %s)", fp_value);\n'%(
|
| 912 |
|
|
instruction["line"],
|
| 913 |
|
|
instruction["file"]))
|
| 914 |
2 |
jondawson |
|
| 915 |
4 |
jondawson |
elif instruction["unsigned"]:
|
| 916 |
2 |
jondawson |
|
| 917 |
4 |
jondawson |
output_file.write(' $display ("%%d (report at line: %s in file: %s)", $unsigned(register_1));\n'%(
|
| 918 |
|
|
instruction["line"],
|
| 919 |
|
|
instruction["file"]))
|
| 920 |
2 |
jondawson |
|
| 921 |
4 |
jondawson |
else:
|
| 922 |
2 |
jondawson |
|
| 923 |
4 |
jondawson |
output_file.write(' $display ("%%d (report at line: %s in file: %s)", $signed(register_1));\n'%(
|
| 924 |
|
|
instruction["line"],
|
| 925 |
|
|
instruction["file"],))
|
| 926 |
2 |
jondawson |
|
| 927 |
4 |
jondawson |
output_file.write(" end\n\n")
|
| 928 |
2 |
jondawson |
|
| 929 |
4 |
jondawson |
elif instruction["op"] == "stop":
|
| 930 |
|
|
#If we are in testbench mode stop the simulation
|
| 931 |
|
|
#If we are part of a larger design, other C programs may still be running
|
| 932 |
|
|
output_file.write(" 16'd%s:\n"%(opcode))
|
| 933 |
|
|
output_file.write(" begin\n")
|
| 934 |
|
|
for file_ in input_files.values():
|
| 935 |
|
|
output_file.write(" $fclose(%s);\n"%file_)
|
| 936 |
|
|
for file_ in output_files.values():
|
| 937 |
|
|
output_file.write(" $fclose(%s);\n"%file_)
|
| 938 |
|
|
if testbench:
|
| 939 |
|
|
output_file.write(' $finish;\n')
|
| 940 |
|
|
output_file.write(" stage_0_enable <= 0;\n")
|
| 941 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 942 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 943 |
|
|
output_file.write(" end\n\n")
|
| 944 |
2 |
jondawson |
|
| 945 |
|
|
|
| 946 |
4 |
jondawson |
output_file.write(" endcase\n")
|
| 947 |
|
|
output_file.write(" end\n")
|
| 948 |
|
|
|
| 949 |
|
|
for instruction in instruction_set:
|
| 950 |
|
|
|
| 951 |
|
|
if instruction["op"] == "read":
|
| 952 |
|
|
output_file.write(" if (s_input_%s_ack == 1'b1 && input_%s_stb == 1'b1) begin\n"%(
|
| 953 |
|
|
instruction["input"],
|
| 954 |
|
|
instruction["input"]))
|
| 955 |
|
|
output_file.write(" result_2 <= input_%s;\n"%(instruction["input"]))
|
| 956 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 957 |
|
|
output_file.write(" s_input_%s_ack <= 1'b0;\n"%instruction["input"])
|
| 958 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 959 |
|
|
output_file.write(" stage_1_enable <= 1;\n")
|
| 960 |
|
|
output_file.write(" stage_2_enable <= 1;\n")
|
| 961 |
|
|
output_file.write(" end\n\n")
|
| 962 |
|
|
|
| 963 |
|
|
elif instruction["op"] == "write":
|
| 964 |
|
|
output_file.write(" if (s_output_%s_stb == 1'b1 && output_%s_ack == 1'b1) begin\n"%(
|
| 965 |
|
|
instruction["output"],
|
| 966 |
|
|
instruction["output"]))
|
| 967 |
|
|
output_file.write(" s_output_%s_stb <= 1'b0;\n"%instruction["output"])
|
| 968 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 969 |
|
|
output_file.write(" stage_1_enable <= 1;\n")
|
| 970 |
|
|
output_file.write(" stage_2_enable <= 1;\n")
|
| 971 |
|
|
output_file.write(" end\n\n")
|
| 972 |
|
|
|
| 973 |
|
|
output_file.write(" if (timer == 0) begin\n")
|
| 974 |
|
|
output_file.write(" if (timer_enable) begin\n")
|
| 975 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 976 |
|
|
output_file.write(" stage_1_enable <= 1;\n")
|
| 977 |
|
|
output_file.write(" stage_2_enable <= 1;\n")
|
| 978 |
|
|
output_file.write(" timer_enable <= 0;\n")
|
| 979 |
|
|
output_file.write(" end\n")
|
| 980 |
|
|
output_file.write(" end else begin\n")
|
| 981 |
|
|
output_file.write(" timer <= timer - 1;\n")
|
| 982 |
|
|
output_file.write(" end\n\n")
|
| 983 |
|
|
|
| 984 |
|
|
|
| 985 |
|
|
if enable_adder:
|
| 986 |
|
|
output_file.write(" if (adder_done) begin\n")
|
| 987 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 988 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 989 |
|
|
output_file.write(" stage_1_enable <= 1;\n")
|
| 990 |
|
|
output_file.write(" stage_2_enable <= 1;\n")
|
| 991 |
|
|
output_file.write(" end\n\n")
|
| 992 |
|
|
|
| 993 |
|
|
if enable_multiplier:
|
| 994 |
|
|
output_file.write(" if (multiplier_done) begin\n")
|
| 995 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 996 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 997 |
|
|
output_file.write(" stage_1_enable <= 1;\n")
|
| 998 |
|
|
output_file.write(" stage_2_enable <= 1;\n")
|
| 999 |
|
|
output_file.write(" end\n\n")
|
| 1000 |
|
|
|
| 1001 |
|
|
if enable_divider:
|
| 1002 |
|
|
output_file.write(" if (divider_done) begin\n")
|
| 1003 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 1004 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 1005 |
|
|
output_file.write(" stage_1_enable <= 1;\n")
|
| 1006 |
|
|
output_file.write(" stage_2_enable <= 1;\n")
|
| 1007 |
|
|
output_file.write(" end\n\n")
|
| 1008 |
|
|
|
| 1009 |
|
|
if enable_int_to_float:
|
| 1010 |
|
|
output_file.write(" if (int_to_float_done) begin\n")
|
| 1011 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 1012 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 1013 |
|
|
output_file.write(" stage_1_enable <= 1;\n")
|
| 1014 |
|
|
output_file.write(" stage_2_enable <= 1;\n")
|
| 1015 |
|
|
output_file.write(" end\n\n")
|
| 1016 |
|
|
|
| 1017 |
|
|
if enable_float_to_int:
|
| 1018 |
|
|
output_file.write(" if (float_to_int_done) begin\n")
|
| 1019 |
|
|
output_file.write(" write_enable_2 <= 1;\n")
|
| 1020 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 1021 |
|
|
output_file.write(" stage_1_enable <= 1;\n")
|
| 1022 |
|
|
output_file.write(" stage_2_enable <= 1;\n")
|
| 1023 |
|
|
output_file.write(" end\n\n")
|
| 1024 |
|
|
|
| 1025 |
|
|
#Reset program counter and control signals
|
| 1026 |
|
|
output_file.write(" if (rst == 1'b1) begin\n")
|
| 1027 |
|
|
output_file.write(" stage_0_enable <= 1;\n")
|
| 1028 |
|
|
output_file.write(" stage_1_enable <= 0;\n")
|
| 1029 |
|
|
output_file.write(" stage_2_enable <= 0;\n")
|
| 1030 |
|
|
output_file.write(" timer <= 0;\n")
|
| 1031 |
|
|
output_file.write(" timer_enable <= 0;\n")
|
| 1032 |
|
|
output_file.write(" program_counter <= 0;\n")
|
| 1033 |
|
|
for i in inputs:
|
| 1034 |
|
|
output_file.write(" s_input_%s_ack <= 0;\n"%(i))
|
| 1035 |
|
|
for i in outputs:
|
| 1036 |
|
|
output_file.write(" s_output_%s_stb <= 0;\n"%(i))
|
| 1037 |
|
|
output_file.write(" end\n")
|
| 1038 |
|
|
output_file.write(" end\n")
|
| 1039 |
|
|
for i in inputs:
|
| 1040 |
|
|
output_file.write(" assign input_%s_ack = s_input_%s_ack;\n"%(i, i))
|
| 1041 |
|
|
for i in outputs:
|
| 1042 |
|
|
output_file.write(" assign output_%s_stb = s_output_%s_stb;\n"%(i, i))
|
| 1043 |
|
|
output_file.write(" assign output_%s = s_output_%s;\n"%(i, i))
|
| 1044 |
|
|
output_file.write("\nendmodule\n")
|
| 1045 |
|
|
|
| 1046 |
|
|
return inputs, outputs
|
| 1047 |
|
|
|
| 1048 |
|
|
def connect_float_to_int(output_file):
|
| 1049 |
|
|
output_file.write(" \n float_to_int float_to_int_1(\n")
|
| 1050 |
|
|
output_file.write(" .clk(clk),\n")
|
| 1051 |
|
|
output_file.write(" .rst(rst),\n")
|
| 1052 |
|
|
output_file.write(" .input_a(float_to),\n")
|
| 1053 |
|
|
output_file.write(" .input_a_stb(float_to_stb),\n")
|
| 1054 |
|
|
output_file.write(" .input_a_ack(float_to_ack),\n")
|
| 1055 |
|
|
output_file.write(" .output_z(to_int),\n")
|
| 1056 |
|
|
output_file.write(" .output_z_stb(to_int_stb),\n")
|
| 1057 |
|
|
output_file.write(" .output_z_ack(to_int_ack)\n")
|
| 1058 |
|
|
output_file.write(" );\n\n")
|
| 1059 |
|
|
output_file.write(" \n always @(posedge clk)\n")
|
| 1060 |
|
|
output_file.write(" begin\n\n")
|
| 1061 |
|
|
output_file.write(" float_to_int_done <= 0;\n")
|
| 1062 |
|
|
output_file.write(" case(float_to_int_state)\n\n")
|
| 1063 |
|
|
output_file.write(" wait_go:\n")
|
| 1064 |
|
|
output_file.write(" begin\n")
|
| 1065 |
|
|
output_file.write(" if (float_to_int_go) begin\n")
|
| 1066 |
|
|
output_file.write(" float_to_int_state <= write_a;\n")
|
| 1067 |
|
|
output_file.write(" end\n")
|
| 1068 |
|
|
output_file.write(" end\n\n")
|
| 1069 |
|
|
output_file.write(" write_a:\n")
|
| 1070 |
|
|
output_file.write(" begin\n")
|
| 1071 |
|
|
output_file.write(" float_to_stb <= 1;\n")
|
| 1072 |
|
|
output_file.write(" if (float_to_stb && float_to_ack) begin\n")
|
| 1073 |
|
|
output_file.write(" float_to_stb <= 0;\n")
|
| 1074 |
|
|
output_file.write(" float_to_int_state <= read_z;\n")
|
| 1075 |
|
|
output_file.write(" end\n")
|
| 1076 |
|
|
output_file.write(" end\n\n")
|
| 1077 |
|
|
output_file.write(" read_z:\n")
|
| 1078 |
|
|
output_file.write(" begin\n")
|
| 1079 |
|
|
output_file.write(" to_int_ack <= 1;\n")
|
| 1080 |
|
|
output_file.write(" if (to_int_stb && to_int_ack) begin\n")
|
| 1081 |
|
|
output_file.write(" to_int_ack <= 0;\n")
|
| 1082 |
|
|
output_file.write(" result_2 <= to_int;\n")
|
| 1083 |
|
|
output_file.write(" float_to_int_state <= wait_go;\n")
|
| 1084 |
|
|
output_file.write(" float_to_int_done <= 1;\n")
|
| 1085 |
|
|
output_file.write(" end\n")
|
| 1086 |
|
|
output_file.write(" end\n")
|
| 1087 |
|
|
output_file.write(" endcase\n")
|
| 1088 |
|
|
output_file.write(" if (rst) begin\n")
|
| 1089 |
|
|
output_file.write(" float_to_int_state <= wait_go;\n")
|
| 1090 |
|
|
output_file.write(" float_to_stb <= 0;\n")
|
| 1091 |
|
|
output_file.write(" to_int_ack <= 0;\n")
|
| 1092 |
|
|
output_file.write(" end\n")
|
| 1093 |
|
|
output_file.write(" end\n\n")
|
| 1094 |
|
|
|
| 1095 |
|
|
def connect_int_to_float(output_file):
|
| 1096 |
|
|
output_file.write(" \n int_to_float int_to_float_1(\n")
|
| 1097 |
|
|
output_file.write(" .clk(clk),\n")
|
| 1098 |
|
|
output_file.write(" .rst(rst),\n")
|
| 1099 |
|
|
output_file.write(" .input_a(int_to),\n")
|
| 1100 |
|
|
output_file.write(" .input_a_stb(int_to_stb),\n")
|
| 1101 |
|
|
output_file.write(" .input_a_ack(int_to_ack),\n")
|
| 1102 |
|
|
output_file.write(" .output_z(to_float),\n")
|
| 1103 |
|
|
output_file.write(" .output_z_stb(to_float_stb),\n")
|
| 1104 |
|
|
output_file.write(" .output_z_ack(to_float_ack)\n")
|
| 1105 |
|
|
output_file.write(" );\n\n")
|
| 1106 |
|
|
output_file.write(" \n always @(posedge clk)\n")
|
| 1107 |
|
|
output_file.write(" begin\n\n")
|
| 1108 |
|
|
output_file.write(" int_to_float_done <= 0;\n")
|
| 1109 |
|
|
output_file.write(" case(int_to_float_state)\n\n")
|
| 1110 |
|
|
output_file.write(" wait_go:\n")
|
| 1111 |
|
|
output_file.write(" begin\n")
|
| 1112 |
|
|
output_file.write(" if (int_to_float_go) begin\n")
|
| 1113 |
|
|
output_file.write(" int_to_float_state <= write_a;\n")
|
| 1114 |
|
|
output_file.write(" end\n")
|
| 1115 |
|
|
output_file.write(" end\n\n")
|
| 1116 |
|
|
output_file.write(" write_a:\n")
|
| 1117 |
|
|
output_file.write(" begin\n")
|
| 1118 |
|
|
output_file.write(" int_to_stb <= 1;\n")
|
| 1119 |
|
|
output_file.write(" if (int_to_stb && int_to_ack) begin\n")
|
| 1120 |
|
|
output_file.write(" int_to_stb <= 0;\n")
|
| 1121 |
|
|
output_file.write(" int_to_float_state <= read_z;\n")
|
| 1122 |
|
|
output_file.write(" end\n")
|
| 1123 |
|
|
output_file.write(" end\n\n")
|
| 1124 |
|
|
output_file.write(" read_z:\n")
|
| 1125 |
|
|
output_file.write(" begin\n")
|
| 1126 |
|
|
output_file.write(" to_float_ack <= 1;\n")
|
| 1127 |
|
|
output_file.write(" if (to_float_stb && to_float_ack) begin\n")
|
| 1128 |
|
|
output_file.write(" to_float_ack <= 0;\n")
|
| 1129 |
|
|
output_file.write(" result_2 <= to_float;\n")
|
| 1130 |
|
|
output_file.write(" int_to_float_state <= wait_go;\n")
|
| 1131 |
|
|
output_file.write(" int_to_float_done <= 1;\n")
|
| 1132 |
|
|
output_file.write(" end\n")
|
| 1133 |
|
|
output_file.write(" end\n")
|
| 1134 |
|
|
output_file.write(" endcase\n")
|
| 1135 |
|
|
output_file.write(" if (rst) begin\n")
|
| 1136 |
|
|
output_file.write(" int_to_float_state <= wait_go;\n")
|
| 1137 |
|
|
output_file.write(" int_to_stb <= 0;\n")
|
| 1138 |
|
|
output_file.write(" to_float_ack <= 0;\n")
|
| 1139 |
|
|
output_file.write(" end\n")
|
| 1140 |
|
|
output_file.write(" end\n\n")
|
| 1141 |
|
|
|
| 1142 |
|
|
def connect_divider(output_file):
|
| 1143 |
|
|
output_file.write(" \n divider divider_1(\n")
|
| 1144 |
|
|
output_file.write(" .clk(clk),\n")
|
| 1145 |
|
|
output_file.write(" .rst(rst),\n")
|
| 1146 |
|
|
output_file.write(" .input_a(divider_a),\n")
|
| 1147 |
|
|
output_file.write(" .input_a_stb(divider_a_stb),\n")
|
| 1148 |
|
|
output_file.write(" .input_a_ack(divider_a_ack),\n")
|
| 1149 |
|
|
output_file.write(" .input_b(divider_b),\n")
|
| 1150 |
|
|
output_file.write(" .input_b_stb(divider_b_stb),\n")
|
| 1151 |
|
|
output_file.write(" .input_b_ack(divider_b_ack),\n")
|
| 1152 |
|
|
output_file.write(" .output_z(divider_z),\n")
|
| 1153 |
|
|
output_file.write(" .output_z_stb(divider_z_stb),\n")
|
| 1154 |
|
|
output_file.write(" .output_z_ack(divider_z_ack)\n")
|
| 1155 |
|
|
output_file.write(" );\n\n")
|
| 1156 |
|
|
output_file.write(" \n always @(posedge clk)\n")
|
| 1157 |
|
|
output_file.write(" begin\n\n")
|
| 1158 |
|
|
output_file.write(" divider_done <= 0;\n")
|
| 1159 |
|
|
output_file.write(" case(div_state)\n\n")
|
| 1160 |
|
|
output_file.write(" wait_go:\n")
|
| 1161 |
|
|
output_file.write(" begin\n")
|
| 1162 |
|
|
output_file.write(" if (divider_go) begin\n")
|
| 1163 |
|
|
output_file.write(" div_state <= write_a;\n")
|
| 1164 |
|
|
output_file.write(" end\n")
|
| 1165 |
|
|
output_file.write(" end\n\n")
|
| 1166 |
|
|
output_file.write(" write_a:\n")
|
| 1167 |
|
|
output_file.write(" begin\n")
|
| 1168 |
|
|
output_file.write(" divider_a_stb <= 1;\n")
|
| 1169 |
|
|
output_file.write(" if (divider_a_stb && divider_a_ack) begin\n")
|
| 1170 |
|
|
output_file.write(" divider_a_stb <= 0;\n")
|
| 1171 |
|
|
output_file.write(" div_state <= write_b;\n")
|
| 1172 |
|
|
output_file.write(" end\n")
|
| 1173 |
|
|
output_file.write(" end\n\n")
|
| 1174 |
|
|
output_file.write(" write_b:\n")
|
| 1175 |
|
|
output_file.write(" begin\n")
|
| 1176 |
|
|
output_file.write(" divider_b_stb <= 1;\n")
|
| 1177 |
|
|
output_file.write(" if (divider_b_stb && divider_b_ack) begin\n")
|
| 1178 |
|
|
output_file.write(" divider_b_stb <= 0;\n")
|
| 1179 |
|
|
output_file.write(" div_state <= read_z;\n")
|
| 1180 |
|
|
output_file.write(" end\n")
|
| 1181 |
|
|
output_file.write(" end\n\n")
|
| 1182 |
|
|
output_file.write(" read_z:\n")
|
| 1183 |
|
|
output_file.write(" begin\n")
|
| 1184 |
|
|
output_file.write(" divider_z_ack <= 1;\n")
|
| 1185 |
|
|
output_file.write(" if (divider_z_stb && divider_z_ack) begin\n")
|
| 1186 |
|
|
output_file.write(" divider_z_ack <= 0;\n")
|
| 1187 |
|
|
output_file.write(" result_2 <= divider_z;\n")
|
| 1188 |
|
|
output_file.write(" div_state <= wait_go;\n")
|
| 1189 |
|
|
output_file.write(" divider_done <= 1;\n")
|
| 1190 |
|
|
output_file.write(" end\n")
|
| 1191 |
|
|
output_file.write(" end\n")
|
| 1192 |
|
|
output_file.write(" endcase\n")
|
| 1193 |
|
|
output_file.write(" if (rst) begin\n")
|
| 1194 |
|
|
output_file.write(" div_state <= wait_go;\n")
|
| 1195 |
|
|
output_file.write(" divider_a_stb <= 0;\n")
|
| 1196 |
|
|
output_file.write(" divider_b_stb <= 0;\n")
|
| 1197 |
|
|
output_file.write(" divider_z_ack <= 0;\n")
|
| 1198 |
|
|
output_file.write(" end\n")
|
| 1199 |
|
|
output_file.write(" end\n\n")
|
| 1200 |
|
|
|
| 1201 |
|
|
def connect_multiplier(output_file):
|
| 1202 |
|
|
output_file.write(" \n multiplier multiplier_1(\n")
|
| 1203 |
|
|
output_file.write(" .clk(clk),\n")
|
| 1204 |
|
|
output_file.write(" .rst(rst),\n")
|
| 1205 |
|
|
output_file.write(" .input_a(multiplier_a),\n")
|
| 1206 |
|
|
output_file.write(" .input_a_stb(multiplier_a_stb),\n")
|
| 1207 |
|
|
output_file.write(" .input_a_ack(multiplier_a_ack),\n")
|
| 1208 |
|
|
output_file.write(" .input_b(multiplier_b),\n")
|
| 1209 |
|
|
output_file.write(" .input_b_stb(multiplier_b_stb),\n")
|
| 1210 |
|
|
output_file.write(" .input_b_ack(multiplier_b_ack),\n")
|
| 1211 |
|
|
output_file.write(" .output_z(multiplier_z),\n")
|
| 1212 |
|
|
output_file.write(" .output_z_stb(multiplier_z_stb),\n")
|
| 1213 |
|
|
output_file.write(" .output_z_ack(multiplier_z_ack)\n")
|
| 1214 |
|
|
output_file.write(" );\n\n")
|
| 1215 |
|
|
output_file.write(" \n always @(posedge clk)\n")
|
| 1216 |
|
|
output_file.write(" begin\n\n")
|
| 1217 |
|
|
output_file.write(" multiplier_done <= 0;\n")
|
| 1218 |
|
|
output_file.write(" case(mul_state)\n\n")
|
| 1219 |
|
|
output_file.write(" wait_go:\n")
|
| 1220 |
|
|
output_file.write(" begin\n")
|
| 1221 |
|
|
output_file.write(" if (multiplier_go) begin\n")
|
| 1222 |
|
|
output_file.write(" mul_state <= write_a;\n")
|
| 1223 |
|
|
output_file.write(" end\n")
|
| 1224 |
|
|
output_file.write(" end\n\n")
|
| 1225 |
|
|
output_file.write(" write_a:\n")
|
| 1226 |
|
|
output_file.write(" begin\n")
|
| 1227 |
|
|
output_file.write(" multiplier_a_stb <= 1;\n")
|
| 1228 |
|
|
output_file.write(" if (multiplier_a_stb && multiplier_a_ack) begin\n")
|
| 1229 |
|
|
output_file.write(" multiplier_a_stb <= 0;\n")
|
| 1230 |
|
|
output_file.write(" mul_state <= write_b;\n")
|
| 1231 |
|
|
output_file.write(" end\n")
|
| 1232 |
|
|
output_file.write(" end\n\n")
|
| 1233 |
|
|
output_file.write(" write_b:\n")
|
| 1234 |
|
|
output_file.write(" begin\n")
|
| 1235 |
|
|
output_file.write(" multiplier_b_stb <= 1;\n")
|
| 1236 |
|
|
output_file.write(" if (multiplier_b_stb && multiplier_b_ack) begin\n")
|
| 1237 |
|
|
output_file.write(" multiplier_b_stb <= 0;\n")
|
| 1238 |
|
|
output_file.write(" mul_state <= read_z;\n")
|
| 1239 |
|
|
output_file.write(" end\n")
|
| 1240 |
|
|
output_file.write(" end\n\n")
|
| 1241 |
|
|
output_file.write(" read_z:\n")
|
| 1242 |
|
|
output_file.write(" begin\n")
|
| 1243 |
|
|
output_file.write(" multiplier_z_ack <= 1;\n")
|
| 1244 |
|
|
output_file.write(" if (multiplier_z_stb && multiplier_z_ack) begin\n")
|
| 1245 |
|
|
output_file.write(" multiplier_z_ack <= 0;\n")
|
| 1246 |
|
|
output_file.write(" result_2 <= multiplier_z;\n")
|
| 1247 |
|
|
output_file.write(" mul_state <= wait_go;\n")
|
| 1248 |
|
|
output_file.write(" multiplier_done <= 1;\n")
|
| 1249 |
|
|
output_file.write(" end\n")
|
| 1250 |
|
|
output_file.write(" end\n\n")
|
| 1251 |
|
|
output_file.write(" endcase\n\n")
|
| 1252 |
|
|
output_file.write(" if (rst) begin\n")
|
| 1253 |
|
|
output_file.write(" mul_state <= wait_go;\n")
|
| 1254 |
|
|
output_file.write(" multiplier_a_stb <= 0;\n")
|
| 1255 |
|
|
output_file.write(" multiplier_b_stb <= 0;\n")
|
| 1256 |
|
|
output_file.write(" multiplier_z_ack <= 0;\n")
|
| 1257 |
|
|
output_file.write(" end\n")
|
| 1258 |
|
|
output_file.write(" end\n\n")
|
| 1259 |
|
|
|
| 1260 |
|
|
def connect_adder(output_file):
|
| 1261 |
|
|
output_file.write(" \n adder adder_1(\n")
|
| 1262 |
|
|
output_file.write(" .clk(clk),\n")
|
| 1263 |
|
|
output_file.write(" .rst(rst),\n")
|
| 1264 |
|
|
output_file.write(" .input_a(adder_a),\n")
|
| 1265 |
|
|
output_file.write(" .input_a_stb(adder_a_stb),\n")
|
| 1266 |
|
|
output_file.write(" .input_a_ack(adder_a_ack),\n")
|
| 1267 |
|
|
output_file.write(" .input_b(adder_b),\n")
|
| 1268 |
|
|
output_file.write(" .input_b_stb(adder_b_stb),\n")
|
| 1269 |
|
|
output_file.write(" .input_b_ack(adder_b_ack),\n")
|
| 1270 |
|
|
output_file.write(" .output_z(adder_z),\n")
|
| 1271 |
|
|
output_file.write(" .output_z_stb(adder_z_stb),\n")
|
| 1272 |
|
|
output_file.write(" .output_z_ack(adder_z_ack)\n")
|
| 1273 |
|
|
output_file.write(" );\n\n")
|
| 1274 |
|
|
output_file.write(" \n always @(posedge clk)\n")
|
| 1275 |
|
|
output_file.write(" begin\n\n")
|
| 1276 |
|
|
output_file.write(" adder_done <= 0;\n")
|
| 1277 |
|
|
output_file.write(" case(add_state)\n\n")
|
| 1278 |
|
|
output_file.write(" wait_go:\n")
|
| 1279 |
|
|
output_file.write(" begin\n")
|
| 1280 |
|
|
output_file.write(" if (adder_go) begin\n")
|
| 1281 |
|
|
output_file.write(" add_state <= write_a;\n")
|
| 1282 |
|
|
output_file.write(" end\n")
|
| 1283 |
|
|
output_file.write(" end\n\n")
|
| 1284 |
|
|
output_file.write(" write_a:\n")
|
| 1285 |
|
|
output_file.write(" begin\n")
|
| 1286 |
|
|
output_file.write(" adder_a_stb <= 1;\n")
|
| 1287 |
|
|
output_file.write(" if (adder_a_stb && adder_a_ack) begin\n")
|
| 1288 |
|
|
output_file.write(" adder_a_stb <= 0;\n")
|
| 1289 |
|
|
output_file.write(" add_state <= write_b;\n")
|
| 1290 |
|
|
output_file.write(" end\n")
|
| 1291 |
|
|
output_file.write(" end\n\n")
|
| 1292 |
|
|
output_file.write(" write_b:\n")
|
| 1293 |
|
|
output_file.write(" begin\n")
|
| 1294 |
|
|
output_file.write(" adder_b_stb <= 1;\n")
|
| 1295 |
|
|
output_file.write(" if (adder_b_stb && adder_b_ack) begin\n")
|
| 1296 |
|
|
output_file.write(" adder_b_stb <= 0;\n")
|
| 1297 |
|
|
output_file.write(" add_state <= read_z;\n")
|
| 1298 |
|
|
output_file.write(" end\n")
|
| 1299 |
|
|
output_file.write(" end\n\n")
|
| 1300 |
|
|
output_file.write(" read_z:\n")
|
| 1301 |
|
|
output_file.write(" begin\n")
|
| 1302 |
|
|
output_file.write(" adder_z_ack <= 1;\n")
|
| 1303 |
|
|
output_file.write(" if (adder_z_stb && adder_z_ack) begin\n")
|
| 1304 |
|
|
output_file.write(" adder_z_ack <= 0;\n")
|
| 1305 |
|
|
output_file.write(" result_2 <= adder_z;\n")
|
| 1306 |
|
|
output_file.write(" add_state <= wait_go;\n")
|
| 1307 |
|
|
output_file.write(" adder_done <= 1;\n")
|
| 1308 |
|
|
output_file.write(" end\n")
|
| 1309 |
|
|
output_file.write(" end\n")
|
| 1310 |
|
|
output_file.write(" endcase\n")
|
| 1311 |
|
|
output_file.write(" if (rst) begin\n")
|
| 1312 |
|
|
output_file.write(" add_state <= wait_go;\n")
|
| 1313 |
|
|
output_file.write(" adder_a_stb <= 0;\n")
|
| 1314 |
|
|
output_file.write(" adder_b_stb <= 0;\n")
|
| 1315 |
|
|
output_file.write(" adder_z_ack <= 0;\n")
|
| 1316 |
|
|
output_file.write(" end\n")
|
| 1317 |
|
|
output_file.write(" end\n\n")
|
| 1318 |
|
|
|
| 1319 |
|
|
def generate_float_to_int_signals(output_file):
|
| 1320 |
|
|
output_file.write(" reg [31:0] float_to;\n")
|
| 1321 |
|
|
output_file.write(" reg float_to_stb;\n")
|
| 1322 |
|
|
output_file.write(" wire float_to_ack;\n")
|
| 1323 |
|
|
output_file.write(" wire [31:0] to_int;\n")
|
| 1324 |
|
|
output_file.write(" wire to_int_stb;\n")
|
| 1325 |
|
|
output_file.write(" reg to_int_ack;\n")
|
| 1326 |
|
|
output_file.write(" reg [1:0] float_to_int_state;\n")
|
| 1327 |
|
|
output_file.write(" reg float_to_int_go;\n")
|
| 1328 |
|
|
output_file.write(" reg float_to_int_done;\n")
|
| 1329 |
|
|
|
| 1330 |
|
|
def generate_int_to_float_signals(output_file):
|
| 1331 |
|
|
output_file.write(" reg [31:0] int_to;\n")
|
| 1332 |
|
|
output_file.write(" reg int_to_stb;\n")
|
| 1333 |
|
|
output_file.write(" wire int_to_ack;\n")
|
| 1334 |
|
|
output_file.write(" wire [31:0] to_float;\n")
|
| 1335 |
|
|
output_file.write(" wire to_float_stb;\n")
|
| 1336 |
|
|
output_file.write(" reg to_float_ack;\n")
|
| 1337 |
|
|
output_file.write(" reg [1:0] int_to_float_state;\n")
|
| 1338 |
|
|
output_file.write(" reg int_to_float_go;\n")
|
| 1339 |
|
|
output_file.write(" reg int_to_float_done;\n")
|
| 1340 |
|
|
|
| 1341 |
|
|
def generate_divider_signals(output_file):
|
| 1342 |
|
|
output_file.write(" reg [31:0] divider_a;\n")
|
| 1343 |
|
|
output_file.write(" reg divider_a_stb;\n")
|
| 1344 |
|
|
output_file.write(" wire divider_a_ack;\n")
|
| 1345 |
|
|
output_file.write(" reg [31:0] divider_b;\n")
|
| 1346 |
|
|
output_file.write(" reg divider_b_stb;\n")
|
| 1347 |
|
|
output_file.write(" wire divider_b_ack;\n")
|
| 1348 |
|
|
output_file.write(" wire [31:0] divider_z;\n")
|
| 1349 |
|
|
output_file.write(" wire divider_z_stb;\n")
|
| 1350 |
|
|
output_file.write(" reg divider_z_ack;\n")
|
| 1351 |
|
|
output_file.write(" reg [1:0] div_state;\n")
|
| 1352 |
|
|
output_file.write(" reg divider_go;\n")
|
| 1353 |
|
|
output_file.write(" reg divider_done;\n")
|
| 1354 |
|
|
|
| 1355 |
|
|
def generate_multiplier_signals(output_file):
|
| 1356 |
|
|
output_file.write(" reg [31:0] multiplier_a;\n")
|
| 1357 |
|
|
output_file.write(" reg multiplier_a_stb;\n")
|
| 1358 |
|
|
output_file.write(" wire multiplier_a_ack;\n")
|
| 1359 |
|
|
output_file.write(" reg [31:0] multiplier_b;\n")
|
| 1360 |
|
|
output_file.write(" reg multiplier_b_stb;\n")
|
| 1361 |
|
|
output_file.write(" wire multiplier_b_ack;\n")
|
| 1362 |
|
|
output_file.write(" wire [31:0] multiplier_z;\n")
|
| 1363 |
|
|
output_file.write(" wire multiplier_z_stb;\n")
|
| 1364 |
|
|
output_file.write(" reg multiplier_z_ack;\n")
|
| 1365 |
|
|
output_file.write(" reg [1:0] mul_state;\n")
|
| 1366 |
|
|
output_file.write(" reg multiplier_go;\n")
|
| 1367 |
|
|
output_file.write(" reg multiplier_done;\n")
|
| 1368 |
|
|
|
| 1369 |
|
|
def generate_adder_signals(output_file):
|
| 1370 |
|
|
output_file.write(" reg [31:0] adder_a;\n")
|
| 1371 |
|
|
output_file.write(" reg adder_a_stb;\n")
|
| 1372 |
|
|
output_file.write(" wire adder_a_ack;\n")
|
| 1373 |
|
|
output_file.write(" reg [31:0] adder_b;\n")
|
| 1374 |
|
|
output_file.write(" reg adder_b_stb;\n")
|
| 1375 |
|
|
output_file.write(" wire adder_b_ack;\n")
|
| 1376 |
|
|
output_file.write(" wire [31:0] adder_z;\n")
|
| 1377 |
|
|
output_file.write(" wire adder_z_stb;\n")
|
| 1378 |
|
|
output_file.write(" reg adder_z_ack;\n")
|
| 1379 |
|
|
output_file.write(" reg [1:0] add_state;\n")
|
| 1380 |
|
|
output_file.write(" reg adder_go;\n")
|
| 1381 |
|
|
output_file.write(" reg adder_done;\n")
|