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

Subversion Repositories tcp_socket

[/] [tcp_socket/] [trunk/] [chips2/] [chips/] [compiler/] [verilog_area.py] - Blame information for rev 2

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

Line No. Rev Author Line
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
def unique(l):
18
 
19
  """In the absence of set in older python implementations, make list values unique"""
20
 
21
  return dict(zip(l, l)).keys()
22
 
23
def log2(instructions):
24
 
25
  """Integer only algorithm to calculate the number of bits needed to store a number"""
26
 
27
  bits = 1
28
  power = 2
29
  while power < instructions:
30
      bits += 1
31
      power *= 2
32
  return bits
33
 
34
def print_verilog_literal(size, value):
35
 
36
    """Print a verilog literal with expicilt size"""
37
 
38
    if(value >= 0):
39
        return "%s'd%s"%(size, value)
40
    else:
41
        return "-%s'd%s"%(size, abs(value))
42
 
43
def remove_register_hazards(instructions):
44
 
45
    """search through instructions, and remove register hazards"""
46
 
47
    wait_2_for = None
48
    wait_1_for = None
49
    new_instructions = []
50
    for instruction in instructions:
51
        wait = 0
52
        if "src" in instruction:
53
            if instruction["src"] == wait_1_for:
54
                wait = max(wait, 1)
55
            if instruction["src"] == wait_2_for:
56
                wait = max(wait, 2)
57
        if "srcb" in instruction:
58
            if instruction["srcb"] == wait_1_for:
59
                wait = max(wait, 1)
60
            if instruction["srcb"] == wait_2_for:
61
                wait = max(wait, 2)
62
        for i in range(wait):
63
             new_instructions.append({"op":"nop"})
64
        new_instructions.append(instruction)
65
 
66
        if instruction["op"] != "label":
67
            wait_1_for = wait_2_for
68
            if "dest" in instruction:
69
                wait_2_for = instruction["dest"]
70
            else:
71
                wait_2_for = None
72
    return new_instructions
73
 
74
def generate_instruction_set(instructions):
75
 
76
    """Calculate the required instruction set"""
77
 
78
    instruction_set = []
79
    instruction_memory = []
80
    for instruction in instructions:
81
        opcode = {}
82
        encoded_instruction = {}
83
        encoded_instruction["dest"] = 0
84
        encoded_instruction["src"] = 0
85
        encoded_instruction["srcb"] = 0
86
        encoded_instruction["literal"] = 0
87
        opcode["op"] = instruction["op"]
88
        opcode["right"] = False
89
        opcode["unsigned"] = False
90
        opcode["literal"] = False
91
 
92
        if "signed" in instruction:
93
            opcode["unsigned"] = not instruction["signed"]
94
 
95
        if "element_size" in instruction:
96
            opcode["element_size"] = instruction["element_size"]
97
 
98
        if "file" in instruction:
99
            opcode["file_"] = instruction["file"]
100
 
101
        if "line" in instruction:
102
            opcode["line"] = instruction["line"]
103
 
104
        if "input" in instruction:
105
            opcode["input"] = instruction["input"]
106
 
107
        if "output" in instruction:
108
            opcode["output"] = instruction["output"]
109
 
110
        if "dest" in instruction:
111
            encoded_instruction["dest"] = instruction["dest"]
112
 
113
        if "src" in instruction:
114
            encoded_instruction["src"] = instruction["src"]
115
 
116
        if "srcb" in instruction:
117
            encoded_instruction["srcb"] = instruction["srcb"]
118
 
119
        if "left" in instruction:
120
            opcode["literal"] = True
121
            encoded_instruction["literal"] = instruction["left"]
122
 
123
        if "right" in instruction:
124
            opcode["literal"] = True
125
            opcode["right"] = True
126
            encoded_instruction["literal"] = instruction["right"]
127
 
128
        if "literal" in instruction:
129
            opcode["literal"] = True
130
            encoded_instruction["literal"] = instruction["literal"]
131
 
132
        if "label" in instruction:
133
            opcode["literal"] = True
134
            encoded_instruction["literal"] = instruction["label"]
135
 
136
        if opcode not in instruction_set:
137
            instruction_set.append(opcode)
138
 
139
        for op, test_opcode in enumerate(instruction_set):
140
            if test_opcode == opcode:
141
                encoded_instruction["op"] = op
142
                encoded_instruction["comment"] = repr(instruction)
143
                instruction_memory.append(encoded_instruction)
144
                break
145
 
146
    return instruction_set, instruction_memory
147
 
148
def calculate_jumps(instructions):
149
 
150
  """change symbolic labels into numeric addresses"""
151
 
152
  #calculate the values of jump locations
153
  location = 0
154
  labels = {}
155
  new_instructions = []
156
  for instruction in instructions:
157
    if instruction["op"] == "label":
158
      labels[instruction["label"]] = location
159
    else:
160
      new_instructions.append(instruction)
161
      location += 1
162
  instructions = new_instructions
163
 
164
  #substitue real values for labeled jump locations
165
  for instruction in instructions:
166
    if "label" in instruction:
167
      instruction["label"]=labels[instruction["label"]]
168
 
169
  return instructions
170
 
171
def generate_declarations(instructions, no_tb_mode, register_bits, opcode_bits):
172
 
173
  """Generate verilog declarations"""
174
 
175
  #list all inputs and outputs used in the program
176
  inputs = unique([i["input"] for i in instructions if "input" in i])
177
  outputs = unique([i["output"] for i in instructions if "output" in i])
178
  input_files = unique([i["file_name"] for i in instructions if "file_read" == i["op"]])
179
  output_files = unique([i["file_name"] for i in instructions if "file_write" == i["op"]])
180
  testbench = not inputs and not outputs and not no_tb_mode
181
 
182
  #Do not generate a port in testbench mode
183
  inports = [
184
    ("input_" + i, 16) for i in inputs
185
  ] + [
186
    ("input_" + i + "_stb", 1) for i in inputs
187
  ] + [
188
    ("output_" + i + "_ack", 1) for i in outputs
189
  ]
190
 
191
  outports = [
192
    ("output_" + i, 16) for i in outputs
193
  ] + [
194
    ("output_" + i + "_stb", 1) for i in outputs
195
  ] + [
196
    ("input_" + i + "_ack", 1) for i in inputs
197
  ]
198
 
199
  #create list of signals
200
  signals = [
201
    ("timer", 16),
202
    ("timer_enable", 1),
203
    ("stage_0_enable", 1),
204
    ("stage_1_enable", 1),
205
    ("stage_2_enable", 1),
206
    ("program_counter", log2(len(instructions))),
207
    ("program_counter_0", log2(len(instructions))),
208
    ("instruction_0", 32 + register_bits*2 + opcode_bits),
209
    ("opcode_0", opcode_bits),
210
    ("dest_0", register_bits),
211
    ("src_0", register_bits),
212
    ("srcb_0", register_bits),
213
    ("literal_0", 32),
214
    ("program_counter_1", log2(len(instructions))),
215
    ("opcode_1", opcode_bits),
216
    ("dest_1", register_bits),
217
    ("register_1", 32),
218
    ("registerb_1", 32),
219
    ("literal_1", 32),
220
    ("dest_2", register_bits),
221
    ("result_2", 32),
222
    ("write_enable_2", 1),
223
    ("address_2", 16),
224
    ("data_out_2", 16),
225
    ("data_in_2", 16),
226
    ("memory_enable_2", 1),
227
    ("address_4", 16),
228
    ("data_out_4", 32),
229
    ("data_in_4", 32),
230
    ("memory_enable_4", 1),
231
  ] + [
232
    ("s_output_" + i + "_stb", 16) for i in outputs
233
  ] + [
234
    ("s_output_" + i, 16) for i in outputs
235
  ] + [
236
    ("s_input_" + i + "_ack", 16) for i in inputs
237
  ]
238
 
239
  if testbench:
240
    signals.append(("clk", 1))
241
    signals.append(("rst", 1))
242
  else:
243
    inports.append(("clk", 1))
244
    inports.append(("rst", 1))
245
 
246
  return inputs, outputs, input_files, output_files, testbench, inports, outports, signals
247
 
248
def generate_CHIP(input_file,
249
                  name,
250
                  instructions,
251
                  output_file,
252
                  registers,
253
                  memory_size_2,
254
                  memory_size_4,
255
                  initialize_memory,
256
                  memory_content_2,
257
                  memory_content_4,
258
                  no_tb_mode=False):
259
 
260
  """A big ugly function to crunch through all the instructions and generate the CHIP equivilent"""
261
 
262
  instructions = remove_register_hazards(instructions)
263
  instructions = calculate_jumps(instructions)
264
  instruction_set, instruction_memory = generate_instruction_set(instructions)
265
  register_bits = log2(len(registers));
266
  opcode_bits = log2(len(instruction_set));
267
  instruction_bits = 32 + register_bits*2 + opcode_bits
268
  declarations = generate_declarations(instructions, no_tb_mode, register_bits, opcode_bits)
269
  inputs, outputs, input_files, output_files, testbench, inports, outports, signals = declarations
270
 
271
  #output the code in verilog
272
  output_file.write("//name : %s\n"%name)
273
  output_file.write("//tag : c components\n")
274
  for i in inputs:
275
      output_file.write("//input : input_%s:16\n"%i)
276
  for i in outputs:
277
      output_file.write("//output : output_%s:16\n"%i)
278
  output_file.write("//source_file : %s\n"%input_file)
279
  output_file.write("///%s\n"%"".join(["=" for i in name]))
280
  output_file.write("///\n")
281
  output_file.write("///*Created by C2CHIP*\n\n")
282
 
283
 
284
  output_file.write("//////////////////////////////////////////////////////////////////////////////\n")
285
  output_file.write("// Register Allocation\n")
286
  output_file.write("// ===================\n")
287
  output_file.write("//   %s   %s   %s  \n"%("Register".center(20), "Name".center(20), "Size".center(20)))
288
  for register, definition in registers.iteritems():
289
      register_name, size = definition
290
      output_file.write("//   %s   %s   %s  \n"%(str(register).center(20), register_name.center(20), str(size).center(20)))
291
 
292
  output_file.write("  \n`timescale 1ns/1ps\n")
293
  output_file.write("module %s"%name)
294
 
295
  all_ports = [name for name, size in inports + outports]
296
  if all_ports:
297
      output_file.write("(")
298
      output_file.write(",".join(all_ports))
299
      output_file.write(");\n")
300
  else:
301
      output_file.write(";\n")
302
 
303
  output_file.write("  integer file_count;\n")
304
 
305
  input_files = dict(zip(input_files, ["input_file_%s"%i for i, j in enumerate(input_files)]))
306
  for i in input_files.values():
307
      output_file.write("  integer %s;\n"%i)
308
 
309
  output_files = dict(zip(output_files, ["output_file_%s"%i for i, j in enumerate(output_files)]))
310
  for i in output_files.values():
311
      output_file.write("  integer %s;\n"%i)
312
 
313
 
314
  def write_declaration(object_type, name, size, value=None):
315
      if size == 1:
316
          output_file.write(object_type)
317
          output_file.write(name)
318
          if value is not None:
319
              output_file.write("= %s'd%s"%(size,value))
320
          output_file.write(";\n")
321
      else:
322
          output_file.write(object_type)
323
          output_file.write("[%i:0]"%(size-1))
324
          output_file.write(" ")
325
          output_file.write(name)
326
          if value is not None:
327
              output_file.write("= %s'd%s"%(size,value))
328
          output_file.write(";\n")
329
 
330
  for name, size in inports:
331
      write_declaration("  input     ", name, size)
332
 
333
  for name, size in outports:
334
      write_declaration("  output    ", name, size)
335
 
336
  for name, size in signals:
337
      write_declaration("  reg       ", name, size)
338
 
339
  memory_size_2 = int(memory_size_2)
340
  memory_size_4 = int(memory_size_4)
341
  if memory_size_2:
342
      output_file.write("  reg [15:0] memory_2 [%i:0];\n"%(memory_size_2-1))
343
  if memory_size_4:
344
      output_file.write("  reg [31:0] memory_4 [%i:0];\n"%(memory_size_4-1))
345
 
346
  output_file.write("  reg [%s:0] instructions [%i:0];\n"%(instruction_bits-1, len(instructions)-1))
347
  output_file.write("  reg [31:0] registers [%i:0];\n"%(len(registers)-1))
348
 
349
 
350
  #generate clock and reset in testbench mode
351
  if testbench:
352
 
353
      output_file.write("\n  //////////////////////////////////////////////////////////////////////////////\n")
354
      output_file.write("  // CLOCK AND RESET GENERATION                                                 \n")
355
      output_file.write("  //                                                                            \n")
356
      output_file.write("  // This file was generated in test bench mode. In this mode, the verilog      \n")
357
      output_file.write("  // output file can be executed directly within a verilog simulator.           \n")
358
      output_file.write("  // In test bench mode, a simulated clock and reset signal are generated within\n")
359
      output_file.write("  // the output file.                                                           \n")
360
      output_file.write("  // Verilog files generated in testbecnch mode are not suitable for synthesis, \n")
361
      output_file.write("  // or for instantiation within a larger design.\n")
362
 
363
      output_file.write("  \n  initial\n")
364
      output_file.write("  begin\n")
365
      output_file.write("    rst <= 1'b1;\n")
366
      output_file.write("    #50 rst <= 1'b0;\n")
367
      output_file.write("  end\n\n")
368
 
369
      output_file.write("  \n  initial\n")
370
      output_file.write("  begin\n")
371
      output_file.write("    clk <= 1'b0;\n")
372
      output_file.write("    while (1) begin\n")
373
      output_file.write("      #5 clk <= ~clk;\n")
374
      output_file.write("    end\n")
375
      output_file.write("  end\n\n")
376
 
377
  #Generate a state machine to execute the instructions
378
  binary_operators = ["+", "-", "*", "/", "|", "&", "^", "<<", ">>", "<",">", ">=",
379
    "<=", "==", "!="]
380
 
381
 
382
  if initialize_memory and (memory_content_2 or memory_content_4):
383
 
384
      output_file.write("\n  //////////////////////////////////////////////////////////////////////////////\n")
385
      output_file.write("  // MEMORY INITIALIZATION                                                      \n")
386
      output_file.write("  //                                                                            \n")
387
      output_file.write("  // In order to reduce program size, array contents have been stored into      \n")
388
      output_file.write("  // memory at initialization. In an FPGA, this will result in the memory being \n")
389
      output_file.write("  // initialized when the FPGA configures.                                      \n")
390
      output_file.write("  // Memory will not be re-initialized at reset.                                \n")
391
      output_file.write("  // Dissable this behaviour using the no_initialize_memory switch              \n")
392
 
393
      output_file.write("  \n  initial\n")
394
      output_file.write("  begin\n")
395
      for location, content in memory_content_2.iteritems():
396
          output_file.write("    memory_2[%s] = %s;\n"%(location, content))
397
      for location, content in memory_content_4.iteritems():
398
          output_file.write("    memory_4[%s] = %s;\n"%(location, content))
399
      output_file.write("  end\n\n")
400
 
401
 
402
  output_file.write("\n  //////////////////////////////////////////////////////////////////////////////\n")
403
  output_file.write("  // INSTRUCTION INITIALIZATION                                                 \n")
404
  output_file.write("  //                                                                            \n")
405
  output_file.write("  // Initialise the contents of the instruction memory                          \n")
406
  output_file.write("  //\n")
407
  output_file.write("  // Intruction Set\n")
408
  output_file.write("  // ==============\n")
409
  for num, opcode in enumerate(instruction_set):
410
      output_file.write("  // %s %s\n"%(num, opcode))
411
 
412
  output_file.write("  // Intructions\n")
413
  output_file.write("  // ===========\n")
414
  output_file.write("  \n  initial\n")
415
  output_file.write("  begin\n")
416
  for location, instruction in enumerate(instruction_memory):
417
      output_file.write("    instructions[%s] = {%s, %s, %s, %s};//%s\n"%(
418
          location,
419
          print_verilog_literal(opcode_bits, instruction["op"]),
420
          print_verilog_literal(register_bits, instruction["dest"]),
421
          print_verilog_literal(register_bits, instruction["src"]),
422
          print_verilog_literal(32, instruction["srcb"] | instruction["literal"]),
423
          instruction["comment"]))
424
  output_file.write("  end\n\n")
425
 
426
  if input_files or output_files:
427
 
428
      output_file.write("\n  //////////////////////////////////////////////////////////////////////////////\n")
429
      output_file.write("  // OPEN FILES                                                                 \n")
430
      output_file.write("  //                                                                            \n")
431
      output_file.write("  // Open all files used at the start of the process                            \n")
432
 
433
      output_file.write("  \n  initial\n")
434
      output_file.write("  begin\n")
435
      for file_name, file_ in input_files.iteritems():
436
          output_file.write("    %s = $fopenr(\"%s\");\n"%(file_, file_name))
437
      for file_name, file_ in output_files.iteritems():
438
          output_file.write("    %s = $fopen(\"%s\");\n"%(file_, file_name))
439
      output_file.write("  end\n\n")
440
 
441
  output_file.write("\n  //////////////////////////////////////////////////////////////////////////////\n")
442
  output_file.write("  // CPU IMPLEMENTAION OF C PROCESS                                             \n")
443
  output_file.write("  //                                                                            \n")
444
  output_file.write("  // This section of the file contains a CPU implementing the C process.        \n")
445
 
446
  output_file.write("  \n  always @(posedge clk)\n")
447
  output_file.write("  begin\n\n")
448
 
449
  if memory_size_2:
450
      output_file.write("    //implement memory for 2 byte x n arrays\n")
451
      output_file.write("    if (memory_enable_2 == 1'b1) begin\n")
452
      output_file.write("      memory_2[address_2] <= data_in_2;\n")
453
      output_file.write("    end\n")
454
      output_file.write("    data_out_2 <= memory_2[address_2];\n")
455
      output_file.write("    memory_enable_2 <= 1'b0;\n\n")
456
 
457
  if memory_size_4:
458
      output_file.write("    //implement memory for 4 byte x n arrays\n")
459
      output_file.write("    if (memory_enable_4 == 1'b1) begin\n")
460
      output_file.write("      memory_4[address_4] <= data_in_4;\n")
461
      output_file.write("    end\n")
462
      output_file.write("    data_out_4 <= memory_4[address_4];\n")
463
      output_file.write("    memory_enable_4 <= 1'b0;\n\n")
464
 
465
  output_file.write("    write_enable_2 <= 0;\n")
466
 
467
  output_file.write("    //stage 0 instruction fetch\n")
468
  output_file.write("    if (stage_0_enable) begin\n")
469
  output_file.write("      stage_1_enable <= 1;\n")
470
  output_file.write("      instruction_0 <= instructions[program_counter];\n")
471
  output_file.write("      opcode_0 = instruction_0[%s:%s];\n"%(
472
      register_bits * 2 + opcode_bits + 31,
473
      register_bits * 2 + 32))
474
  output_file.write("      dest_0 = instruction_0[%s:%s];\n"%(
475
      register_bits * 2 + 31,
476
      register_bits + 32))
477
  output_file.write("      src_0 = instruction_0[%s:32];\n"%(
478
      register_bits + 31))
479
  output_file.write("      srcb_0 = instruction_0[%s:0];\n"%(register_bits-1))
480
  output_file.write("      literal_0 = instruction_0[31:0];\n")
481
  output_file.write("      if(write_enable_2) begin\n")
482
  output_file.write("        registers[dest_2] <= result_2;\n")
483
  output_file.write("      end\n")
484
  output_file.write("      program_counter_0 <= program_counter;\n")
485
  output_file.write("      program_counter <= program_counter + 1;\n")
486
  output_file.write("    end\n\n")
487
 
488
  output_file.write("    //stage 1 opcode fetch\n")
489
  output_file.write("    if (stage_1_enable) begin\n")
490
  output_file.write("      stage_2_enable <= 1;\n")
491
  output_file.write("      register_1 <= registers[src_0];\n")
492
  output_file.write("      registerb_1 <= registers[srcb_0];\n")
493
  output_file.write("      dest_1 <= dest_0;\n")
494
  output_file.write("      literal_1 <= literal_0;\n")
495
  output_file.write("      opcode_1 <= opcode_0;\n")
496
  output_file.write("      program_counter_1 <= program_counter_0;\n")
497
  output_file.write("    end\n\n")
498
 
499
  output_file.write("    //stage 2 opcode fetch\n")
500
  output_file.write("    if (stage_2_enable) begin\n")
501
  output_file.write("      dest_2 <= dest_1;\n")
502
  output_file.write("      case(opcode_1)\n\n")
503
 
504
  #A frame is executed in each state
505
  for opcode, instruction in enumerate(instruction_set):
506
 
507
      if instruction["op"] == "literal":
508
        output_file.write("        16'd%s:\n"%(opcode))
509
        output_file.write("        begin\n")
510
        output_file.write("          result_2 <= literal_1;\n")
511
        output_file.write("          write_enable_2 <= 1;\n")
512
        output_file.write("        end\n\n")
513
 
514
      elif instruction["op"] == "move":
515
        output_file.write("        16'd%s:\n"%(opcode))
516
        output_file.write("        begin\n")
517
        output_file.write("          result_2 <= register_1;\n")
518
        output_file.write("          write_enable_2 <= 1;\n")
519
        output_file.write("        end\n\n")
520
 
521
      elif instruction["op"] == "~":
522
        output_file.write("        16'd%s:\n"%(opcode))
523
        output_file.write("        begin\n")
524
        output_file.write("          result_2 <= ~register_1;\n")
525
        output_file.write("          write_enable_2 <= 1;\n")
526
        output_file.write("        end\n\n")
527
 
528
      elif instruction["op"] in binary_operators:
529
            if instruction["literal"]:
530
                if instruction["unsigned"]:
531
                    output_file.write("        16'd%s:\n"%(opcode))
532
                    output_file.write("        begin\n")
533
                    if instruction["right"]:
534
                        output_file.write("          result_2 <= $unsigned(register_1) %s $unsigned(literal_1);\n"%(instruction["op"]))
535
                    else:
536
                        output_file.write("          result_2 <= $unsigned(literal_1) %s $unsigned(register_1);\n"%(instruction["op"]))
537
                    output_file.write("          write_enable_2 <= 1;\n")
538
                    output_file.write("        end\n\n")
539
                else:
540
                    if instruction["op"] == ">>":
541
                        instruction["op"] = ">>>"
542
                    output_file.write("        16'd%s:\n"%(opcode))
543
                    output_file.write("        begin\n")
544
                    if instruction["right"]:
545
                        output_file.write("          result_2 <= $signed(register_1) %s $signed(literal_1);\n"%(instruction["op"]))
546
                    else:
547
                        output_file.write("          result_2 <= $signed(literal_1) %s $signed(register_1);\n"%(instruction["op"]))
548
                    output_file.write("          write_enable_2 <= 1;\n")
549
                    output_file.write("        end\n\n")
550
            else:
551
                if instruction["unsigned"]:
552
                    output_file.write("        16'd%s:\n"%(opcode))
553
                    output_file.write("        begin\n")
554
                    output_file.write("          result_2 <= $unsigned(register_1) %s $unsigned(registerb_1);\n"%(instruction["op"]))
555
                    output_file.write("          write_enable_2 <= 1;\n")
556
                    output_file.write("        end\n\n")
557
                else:
558
                    if instruction["op"] == ">>":
559
                        instruction["op"] = ">>>"
560
                    output_file.write("        16'd%s:\n"%(opcode))
561
                    output_file.write("        begin\n")
562
                    output_file.write("          result_2 <= $signed(register_1) %s $signed(registerb_1);\n"%(instruction["op"]))
563
                    output_file.write("          write_enable_2 <= 1;\n")
564
                    output_file.write("        end\n\n")
565
 
566
      elif instruction["op"] == "jmp_if_false":
567
        output_file.write("        16'd%s:\n"%(opcode))
568
        output_file.write("        begin\n")
569
        output_file.write("          if (register_1 == 0) begin\n");
570
        output_file.write("            program_counter <= literal_1;\n")
571
        output_file.write("            stage_0_enable <= 1;\n")
572
        output_file.write("            stage_1_enable <= 0;\n")
573
        output_file.write("            stage_2_enable <= 0;\n")
574
        output_file.write("          end\n")
575
        output_file.write("        end\n\n")
576
 
577
      elif instruction["op"] == "jmp_if_true":
578
        output_file.write("        16'd%s:\n"%(opcode))
579
        output_file.write("        begin\n")
580
        output_file.write("          if (register_1 != 0) begin\n");
581
        output_file.write("            program_counter <= literal_1;\n")
582
        output_file.write("            stage_0_enable <= 1;\n")
583
        output_file.write("            stage_1_enable <= 0;\n")
584
        output_file.write("            stage_2_enable <= 0;\n")
585
        output_file.write("          end\n")
586
        output_file.write("        end\n\n")
587
 
588
      elif instruction["op"] == "jmp_and_link":
589
        output_file.write("        16'd%s:\n"%(opcode))
590
        output_file.write("        begin\n")
591
        output_file.write("          program_counter <= literal_1;\n")
592
        output_file.write("          result_2 <= program_counter_1 + 1;\n")
593
        output_file.write("          write_enable_2 <= 1;\n")
594
        output_file.write("          stage_0_enable <= 1;\n")
595
        output_file.write("          stage_1_enable <= 0;\n")
596
        output_file.write("          stage_2_enable <= 0;\n")
597
        output_file.write("        end\n\n")
598
 
599
      elif instruction["op"] == "jmp_to_reg":
600
        output_file.write("        16'd%s:\n"%(opcode))
601
        output_file.write("        begin\n")
602
        output_file.write("          program_counter <= register_1;\n")
603
        output_file.write("          stage_0_enable <= 1;\n")
604
        output_file.write("          stage_1_enable <= 0;\n")
605
        output_file.write("          stage_2_enable <= 0;\n")
606
        output_file.write("        end\n\n")
607
 
608
      elif instruction["op"] == "goto":
609
        output_file.write("        16'd%s:\n"%(opcode))
610
        output_file.write("        begin\n")
611
        output_file.write("          program_counter <= literal_1;\n")
612
        output_file.write("          stage_0_enable <= 1;\n")
613
        output_file.write("          stage_1_enable <= 0;\n")
614
        output_file.write("          stage_2_enable <= 0;\n")
615
        output_file.write("        end\n\n")
616
 
617
      elif instruction["op"] == "file_read":
618
        output_file.write("        16'd%s:\n"%(opcode))
619
        output_file.write("        begin\n")
620
        output_file.write("          file_count = $fscanf(%s, \"%%d\\n\", result_2);\n"%(
621
          input_files[instruction["file_"]]))
622
        output_file.write("          write_enable_2 <= 1;\n")
623
        output_file.write("        end\n\n")
624
 
625
      elif instruction["op"] == "file_write":
626
        output_file.write("        16'd%s:\n"%(opcode))
627
        output_file.write("        begin\n")
628
        output_file.write("          $fdisplay(%s, \"%%d\", register_1);\n"%(
629
          output_files[instruction["file_name"]]))
630
        output_file.write("        end\n\n")
631
 
632
      elif instruction["op"] == "read":
633
        output_file.write("        16'd%s:\n"%(opcode))
634
        output_file.write("        begin\n")
635
        output_file.write("          stage_0_enable <= 0;\n")
636
        output_file.write("          stage_1_enable <= 0;\n")
637
        output_file.write("          stage_2_enable <= 0;\n")
638
        output_file.write("          s_input_%s_ack <= 1'b1;\n"%instruction["input"])
639
        output_file.write("        end\n\n")
640
 
641
      elif instruction["op"] == "ready":
642
        output_file.write("        16'd%s:\n"%(opcode))
643
        output_file.write("        begin\n")
644
        output_file.write("          result_2 <= 0;\n")
645
        output_file.write("          result_2[0] <= input_%s_stb;\n"%(
646
          instruction["input"]))
647
        output_file.write("          write_enable_2 <= 1;\n")
648
        output_file.write("        end\n\n")
649
 
650
      elif instruction["op"] == "write":
651
        output_file.write("        16'd%s:\n"%(opcode))
652
        output_file.write("        begin\n")
653
        output_file.write("          stage_0_enable <= 0;\n")
654
        output_file.write("          stage_1_enable <= 0;\n")
655
        output_file.write("          stage_2_enable <= 0;\n")
656
        output_file.write("          s_output_%s_stb <= 1'b1;\n"%instruction["output"])
657
        output_file.write("          s_output_%s <= register_1;\n"%instruction["output"])
658
        output_file.write("        end\n\n")
659
 
660
      elif instruction["op"] == "memory_read_request":
661
        output_file.write("        16'd%s:\n"%(opcode))
662
        output_file.write("        begin\n")
663
        output_file.write("          address_%s <= register_1;\n"%(
664
          instruction["element_size"]))
665
        output_file.write("        end\n\n")
666
 
667
      elif instruction["op"] == "memory_read_wait":
668
          pass
669
 
670
      elif instruction["op"] == "memory_read":
671
        output_file.write("        16'd%s:\n"%(opcode))
672
        output_file.write("        begin\n")
673
        output_file.write("          result_2 <= data_out_%s;\n"%(
674
          instruction["element_size"]))
675
        output_file.write("          write_enable_2 <= 1;\n")
676
        output_file.write("        end\n\n")
677
 
678
      elif instruction["op"] == "memory_write":
679
        output_file.write("        16'd%s:\n"%(opcode))
680
        output_file.write("        begin\n")
681
        output_file.write("          address_%s <= register_1;\n"%(
682
          instruction["element_size"]))
683
        output_file.write("          data_in_%s <= registerb_1;\n"%(
684
          instruction["element_size"]))
685
        output_file.write("          memory_enable_%s <= 1'b1;\n"%(
686
          instruction["element_size"]))
687
        output_file.write("        end\n\n")
688
 
689
      elif instruction["op"] == "assert":
690
        output_file.write("        16'd%s:\n"%(opcode))
691
        output_file.write("        begin\n")
692
        output_file.write("          if (register_1 == 0) begin\n")
693
        output_file.write("            $display(\"Assertion failed at line: %s in file: %s\");\n"%(
694
          instruction["line"],
695
          instruction["file_"]))
696
        output_file.write("            $finish_and_return(1);\n")
697
        output_file.write("          end\n")
698
        output_file.write("        end\n\n")
699
 
700
      elif instruction["op"] == "wait_clocks":
701
        output_file.write("        16'd%s:\n"%(opcode))
702
        output_file.write("        begin\n")
703
        output_file.write("          timer <= register_1;\n")
704
        output_file.write("          timer_enable <= 1;\n")
705
        output_file.write("          stage_0_enable <= 0;\n")
706
        output_file.write("          stage_1_enable <= 0;\n")
707
        output_file.write("          stage_2_enable <= 0;\n")
708
        output_file.write("        end\n\n")
709
 
710
      elif instruction["op"] == "report":
711
        output_file.write("        16'd%s:\n"%(opcode))
712
        output_file.write("        begin\n")
713
        if instruction["unsigned"]:
714
            output_file.write('          $display ("%%d (report at line: %s in file: %s)", $unsigned(register_1));\n'%(
715
              instruction["line"],
716
              instruction["file_"]))
717
        else:
718
            output_file.write('          $display ("%%d (report at line: %s in file: %s)", $signed(register_1));\n'%(
719
              instruction["line"],
720
              instruction["file_"],))
721
        output_file.write("        end\n\n")
722
 
723
      elif instruction["op"] == "stop":
724
        #If we are in testbench mode stop the simulation
725
        #If we are part of a larger design, other C programs may still be running
726
        output_file.write("        16'd%s:\n"%(opcode))
727
        output_file.write("        begin\n")
728
        for file_ in input_files.values():
729
            output_file.write("          $fclose(%s);\n"%file_)
730
        for file_ in output_files.values():
731
            output_file.write("          $fclose(%s);\n"%file_)
732
        if testbench:
733
            output_file.write('          $finish;\n')
734
        output_file.write("          stage_0_enable <= 0;\n")
735
        output_file.write("          stage_1_enable <= 0;\n")
736
        output_file.write("          stage_2_enable <= 0;\n")
737
        output_file.write("        end\n\n")
738
 
739
 
740
  output_file.write("       endcase\n")
741
  output_file.write("    end\n")
742
 
743
  for instruction in instruction_set:
744
 
745
      if instruction["op"] == "read":
746
        output_file.write("    if (s_input_%s_ack == 1'b1 && input_%s_stb == 1'b1) begin\n"%(
747
          instruction["input"],
748
          instruction["input"]))
749
        output_file.write("       result_2 <= input_%s;\n"%(instruction["input"]))
750
        output_file.write("       write_enable_2 <= 1;\n")
751
        output_file.write("       s_input_%s_ack <= 1'b0;\n"%instruction["input"])
752
        output_file.write("       stage_0_enable <= 1;\n")
753
        output_file.write("       stage_1_enable <= 1;\n")
754
        output_file.write("       stage_2_enable <= 1;\n")
755
        output_file.write("     end\n\n")
756
 
757
      elif instruction["op"] == "write":
758
        output_file.write("     if (s_output_%s_stb == 1'b1 && output_%s_ack == 1'b1) begin\n"%(
759
          instruction["output"],
760
          instruction["output"]))
761
        output_file.write("       s_output_%s_stb <= 1'b0;\n"%instruction["output"])
762
        output_file.write("       stage_0_enable <= 1;\n")
763
        output_file.write("       stage_1_enable <= 1;\n")
764
        output_file.write("       stage_2_enable <= 1;\n")
765
        output_file.write("     end\n\n")
766
 
767
  output_file.write("    if (timer == 0) begin\n")
768
  output_file.write("      if (timer_enable) begin\n")
769
  output_file.write("         stage_0_enable <= 1;\n")
770
  output_file.write("         stage_1_enable <= 1;\n")
771
  output_file.write("         stage_2_enable <= 1;\n")
772
  output_file.write("         timer_enable <= 0;\n")
773
  output_file.write("      end\n")
774
  output_file.write("    end else begin\n")
775
  output_file.write("      timer <= timer - 1;\n")
776
  output_file.write("    end\n\n")
777
 
778
  #Reset program counter and control signals
779
  output_file.write("    if (rst == 1'b1) begin\n")
780
  output_file.write("      stage_0_enable <= 1;\n")
781
  output_file.write("      stage_1_enable <= 0;\n")
782
  output_file.write("      stage_2_enable <= 0;\n")
783
  output_file.write("      timer <= 0;\n")
784
  output_file.write("      timer_enable <= 0;\n")
785
  output_file.write("      program_counter <= 0;\n")
786
  for i in inputs:
787
      output_file.write("      s_input_%s_ack <= 0;\n"%(i))
788
  for i in outputs:
789
      output_file.write("      s_output_%s_stb <= 0;\n"%(i))
790
  output_file.write("    end\n")
791
  output_file.write("  end\n")
792
  for i in inputs:
793
    output_file.write("  assign input_%s_ack = s_input_%s_ack;\n"%(i, i))
794
  for i in outputs:
795
    output_file.write("  assign output_%s_stb = s_output_%s_stb;\n"%(i, i))
796
    output_file.write("  assign output_%s = s_output_%s;\n"%(i, i))
797
  output_file.write("\nendmodule\n")
798
 
799
  return inputs, outputs

powered by: WebSVN 2.1.0

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