-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystem.scala
165 lines (141 loc) · 4.73 KB
/
System.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright 2023 Frank Zeyda
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package j1.chisel
import j1.examples.ChaserLight3
import circt.stage.ChiselStage
import chisel3._
import chisel3.util._
import scala.io.AnsiColor._
/* Example system with three connected LEDS. */
class j1System(clk_freq: Int = 50000000)(implicit cfg: j1Config)
extends Module {
// Configuration
import cfg.{datawidth,dstkDepth,rstkDepth,memsize,use_bb_tdp}
// Interface
val io = IO(new Bundle {
val led0 = Output(Bool())
val led1 = Output(Bool())
val led2 = Output(Bool())
})
// Test Probes
val probe = IO(new Bundle {
val pc = Output(UInt(16.W))
val reboot = Output(Bool())
val dsp = Output(UInt(dstkDepth.W))
val rsp = Output(UInt(rstkDepth.W))
val st0 = Output(UInt(datawidth.W))
val st1 = Output(UInt(datawidth.W))
val status = Output(Bits(3.W))
})
// Submodules
val j1cpu = Module(new j1)
val memory: DualPortedRAM = {
if (use_bb_tdp) {
Module(new BBDualPortedRAM(datawidth, memsize))
}
else {
Module(new ChiselDualPortedRAM(datawidth, memsize))
}
}
// Connections: Memory Inputs
memory.io.clock := clock // only used by BB module
memory.io.rdAddrA := j1cpu.io.codeaddr
memory.io.wrAddrA := DontCare // UNUSED
memory.io.rdAddrB := j1cpu.io.mem_addr
memory.io.wrAddrB := j1cpu.io.mem_addr
memory.io.wrDataA := DontCare // UNSUED
memory.io.wrDataB := j1cpu.io.dout
memory.io.wrEnaA := false.B
memory.io.wrEnaB := j1cpu.io.mem_wr
// Connections: J1 CPU Inputs
j1cpu.io.insn := memory.io.rdDataA
j1cpu.io.mem_din := memory.io.rdDataB
// Connections: Test Probes
probe.pc := j1cpu.probe.pc
probe.reboot := j1cpu.probe.reboot
probe.status := j1cpu.probe.status
probe.dsp := j1cpu.probe.dsp
probe.rsp := j1cpu.probe.rsp
probe.st0 := j1cpu.probe.st0
probe.st1 := j1cpu.probe.st1
// Status Flashing
val status_flash = RegInit(false.B)
/* Create a counter for flashing of the status LEDs. */
val (_, counterWrap) = Counter(true.B, clk_freq / 4)
// Toggle status_flashing register periodically.
when (counterWrap) {
status_flash := ~status_flash
}
// LEDS Device Interface
val leds_state = RegInit(7.U(3.W))
// External Connections
when (j1cpu.io.status === j1Status.RUNNING) {
io.led0 := leds_state(0)
io.led1 := leds_state(1)
io.led2 := leds_state(2)
}
.otherwise {
io.led0 := j1cpu.io.status(0) && status_flash
io.led1 := j1cpu.io.status(1) && status_flash
io.led2 := j1cpu.io.status(2) && status_flash
}
/**************/
/* IO Mapping */
/**************/
/* Buffer j1cpu comibatorial outputs to delay device writes. */
val io_addr = RegNext(next = j1cpu.io.mem_addr, init = 0.U)
val io_dout = RegNext(next = j1cpu.io.dout, init = 0.U)
val io_wr = RegNext(next = j1cpu.io.io_wr , init = false.B)
/* IO Mapping: Read Action */
j1cpu.io.io_din := 0.U
switch (j1cpu.io.dout) {
is ("h0000".U) {
j1cpu.io.io_din := leds_state
}
}
/* IO Mapping: Write Action */
when (io_wr) {
switch (io_addr) {
is ("h0000".U) {
leds_state := io_dout
}
}
}
}
object j1SystemGen extends App {
import j1.utils.Output
/* Step 1: Load design configuration from j1.conf properties file. */
Output.info(f"Loading configuration from '${BOLD}j1.conf${RESET}' " +
"properties file ...")
/* For generation, ensure that we are using black-box TDP RAM. */
implicit val cfg = j1Config.load("j1.conf").with_bb_tdp
cfg.dump()
/* Step 2: Generate SystemVerilog files for the Chisel design. */
Output.info(
f"Generating Verilog files inside '${BOLD}generated${RESET}' folder ...")
ChiselStage.emitSystemVerilogFile(new j1System,
Array("--target-dir", "dummy"),
Array("--strip-debug-info",
"--disable-all-randomization",
"--split-verilog", "-o", "generated"))
/* Step 3: Create memory initialization file for chaser light program. */
Output.info(f"Creating memory initialization file for example program" +
f" (${RED}ChaserLight3${RESET})")
ChaserLight3.disasm()
ChaserLight3.writehex("generated/meminit.hex")
Output.info(
f"Find all generated files inside the '${BOLD}generated${RESET}' folder.")
}