lab2打包

This commit is contained in:
2025-11-18 20:25:21 +08:00
parent 560a9add68
commit 45dc51e4c4
39 changed files with 3394 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
// Copyright 2021 Howard Lau
//
// 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 board.basys3
import chisel3._
import chisel3.util._
class BCD2Segments extends Module {
val io = IO(new Bundle {
val bcd = Input(UInt(4.W))
val segs = Output(UInt(8.W))
})
val bcd = io.bcd
val segs = Wire(UInt(8.W))
segs := MuxLookup(
bcd,
0xFF.U,
IndexedSeq(
0.U -> "b10000001".U,
1.U -> "b11001111".U,
2.U -> "b10010010".U,
3.U -> "b10000110".U,
4.U -> "b11001100".U,
5.U -> "b10100100".U,
6.U -> "b10100000".U,
7.U -> "b10001111".U,
8.U -> "b10000000".U,
9.U -> "b10000100".U,
10.U -> "b00001000".U,
11.U -> "b01100000".U,
12.U -> "b00110001".U,
13.U -> "b01000010".U,
14.U -> "b00110000".U,
15.U -> "b00111000".U,
)
)
io.segs := segs
}

View File

@@ -0,0 +1,32 @@
// Copyright 2021 Howard Lau
//
// 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 board.basys3
import chisel3._
class OnboardDigitDisplay extends Module {
val io = IO(new Bundle {
val digit_mask = Output(UInt(4.W))
})
val counter = RegInit(UInt(16.W), 0.U)
val digit_mask = RegInit(UInt(4.W), "b0111".U)
counter := counter + 1.U
when(counter === 0.U) {
digit_mask := (digit_mask << 1.U).asUInt + digit_mask(3)
}
io.digit_mask := digit_mask
}

View File

@@ -0,0 +1,34 @@
// Copyright 2021 Howard Lau
//
// 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 board.basys3
import chisel3._
import chisel3.util._
class SYSULogo extends Module {
val io = IO(new Bundle {
val digit_mask = Input(UInt(4.W))
val segs = Output(UInt(8.W))
})
io.segs := MuxLookup(
io.digit_mask,
"b00100100".U, // "b0111".U, "b1101".U -> S
IndexedSeq(
"b1011".U -> "b01000100".U, // Y
"b1110".U -> "b01000001".U, // U
)
)
}

View File

@@ -0,0 +1,42 @@
// Copyright 2021 Howard Lau
//
// 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 board.basys3
import chisel3._
import chisel3.util._
class SegmentMux extends Module {
val io = IO(new Bundle {
val digit_mask = Input(UInt(4.W))
val numbers = Input(UInt(16.W))
val segs = Output(UInt(8.W))
})
val digit = RegInit(UInt(4.W), 0.U)
val bcd2segs = Module(new BCD2Segments)
bcd2segs.io.bcd := digit
io.segs := bcd2segs.io.segs
digit := MuxLookup(
io.digit_mask,
io.numbers(3, 0), // "b1110".U
IndexedSeq(
"b1101".U -> io.numbers(7, 4),
"b1011".U -> io.numbers(11, 8),
"b0111".U -> io.numbers(15, 12)
)
)
}

View File

@@ -0,0 +1,138 @@
// Copyright 2021 Howard Lau
//
// 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 board.basys3
import chisel3._
import chisel3.experimental.ChiselEnum
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}
import chisel3.util._
import peripheral.{CharacterDisplay, Dummy, InstructionROM, Memory, ROMLoader, Uart, VGADisplay, Timer}
import riscv._
import riscv.core.{CPU, ProgramCounter}
object BootStates extends ChiselEnum {
val Init, Loading, Finished = Value
}
class Top extends Module {
val binaryFilename = "tetris.asmbin"
val io = IO(new Bundle {
val switch = Input(UInt(16.W))
val segs = Output(UInt(8.W))
val digit_mask = Output(UInt(4.W))
val hsync = Output(Bool())
val vsync = Output(Bool())
val rgb = Output(UInt(12.W))
val led = Output(UInt(16.W))
val tx = Output(Bool())
val rx = Input(Bool())
})
val mem = Module(new Memory(Parameters.MemorySizeInWords))
val vga_display = Module(new VGADisplay)
val display = Module(new CharacterDisplay)
val timer = Module(new Timer)
val uart = Module(new Uart(frequency = 100000000, baudRate = 115200))
val dummy = Module(new Dummy)
display.io.bundle <> dummy.io.bundle
mem.io.bundle <> dummy.io.bundle
mem.io.debug_read_address := 0.U
timer.io.bundle <> dummy.io.bundle
uart.io.bundle <> dummy.io.bundle
io.tx := uart.io.txd
uart.io.rxd := io.rx
val instruction_rom = Module(new InstructionROM(binaryFilename))
val rom_loader = Module(new ROMLoader(instruction_rom.capacity))
rom_loader.io.rom_data := instruction_rom.io.data
rom_loader.io.load_address := Parameters.EntryAddress
instruction_rom.io.address := rom_loader.io.rom_address
val CPU_clkdiv = RegInit(UInt(2.W), 0.U)
val CPU_tick = Wire(Bool())
val CPU_next = Wire(UInt(2.W))
CPU_next := Mux(CPU_clkdiv === 3.U, 0.U, CPU_clkdiv + 1.U)
CPU_tick := CPU_clkdiv === 0.U
CPU_clkdiv := CPU_next
withClock(CPU_tick.asClock) {
val cpu = Module(new CPU)
cpu.io.interrupt_flag := Cat(uart.io.signal_interrupt, timer.io.signal_interrupt)
cpu.io.csr_regs_debug_read_address := 0.U
cpu.io.regs_debug_read_address := 0.U
cpu.io.instruction_valid := rom_loader.io.load_finished
mem.io.instruction_address := cpu.io.instruction_address
cpu.io.instruction := mem.io.instruction
when(!rom_loader.io.load_finished) {
rom_loader.io.bundle <> mem.io.bundle
cpu.io.memory_bundle.read_data := 0.U
}.otherwise {
rom_loader.io.bundle.read_data := 0.U
when(cpu.io.deviceSelect === 4.U) {
cpu.io.memory_bundle <> timer.io.bundle
}.elsewhen(cpu.io.deviceSelect === 2.U) {
cpu.io.memory_bundle <> uart.io.bundle
}.elsewhen(cpu.io.deviceSelect === 1.U) {
cpu.io.memory_bundle <> display.io.bundle
}.otherwise {
cpu.io.memory_bundle <> mem.io.bundle
}
}
}
display.io.x := vga_display.io.x
display.io.y := vga_display.io.y
io.hsync := vga_display.io.hsync
io.vsync := vga_display.io.vsync
io.rgb := display.io.rgb
mem.io.debug_read_address := io.switch(15, 1).asUInt << 2
io.led := Mux(
io.switch(0),
mem.io.debug_read_data(31, 16).asUInt,
mem.io.debug_read_data(15, 0).asUInt,
)
val onboard_display = Module(new OnboardDigitDisplay)
io.digit_mask := onboard_display.io.digit_mask
val sysu_logo = Module(new SYSULogo)
sysu_logo.io.digit_mask := io.digit_mask
val seg_mux = Module(new SegmentMux)
seg_mux.io.digit_mask := io.digit_mask
seg_mux.io.numbers := io.led
io.segs := MuxLookup(
io.switch,
seg_mux.io.segs,
IndexedSeq(
0.U -> sysu_logo.io.segs
)
)
}
object VerilogGenerator extends App {
(new ChiselStage).execute(Array("-X", "verilog", "-td", "verilog/basys3"), Seq(ChiselGeneratorAnnotation(() => new Top)))
}

View File

@@ -0,0 +1,109 @@
// Copyright 2022 Canbin Huang
//
// 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 board.pynq
import chisel3._
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}
import chisel3.util.Cat
import peripheral._
import riscv.Parameters
import riscv.core.{CPU, ProgramCounter}
class Top extends Module {
val binaryFilename = "hello.asmbin"
val io = IO(new Bundle() {
val hdmi_clk_n = Output(Bool())
val hdmi_clk_p = Output(Bool())
val hdmi_data_n = Output(UInt(3.W))
val hdmi_data_p = Output(UInt(3.W))
val hdmi_hpdn = Output(Bool())
val tx = Output(Bool())
val rx = Input(Bool())
val led = Output(UInt(4.W))
})
val mem = Module(new Memory(Parameters.MemorySizeInWords))
val hdmi_display = Module(new HDMIDisplay)
val display = Module(new CharacterDisplay)
val timer = Module(new Timer)
val uart = Module(new Uart(frequency = 125000000, baudRate = 115200))
val dummy = Module(new Dummy)
display.io.bundle <> dummy.io.bundle
mem.io.bundle <> dummy.io.bundle
mem.io.debug_read_address := 0.U
timer.io.bundle <> dummy.io.bundle
uart.io.bundle <> dummy.io.bundle
io.tx := uart.io.txd
uart.io.rxd := io.rx
val instruction_rom = Module(new InstructionROM(binaryFilename))
val rom_loader = Module(new ROMLoader(instruction_rom.capacity))
rom_loader.io.rom_data := instruction_rom.io.data
rom_loader.io.load_address := Parameters.EntryAddress
instruction_rom.io.address := rom_loader.io.rom_address
val CPU_clkdiv = RegInit(UInt(2.W),0.U)
val CPU_tick = Wire(Bool())
val CPU_next = Wire(UInt(2.W))
CPU_next := Mux(CPU_clkdiv === 3.U, 0.U, CPU_clkdiv + 1.U)
CPU_tick := CPU_clkdiv === 0.U
CPU_clkdiv := CPU_next
withClock(CPU_tick.asClock) {
val cpu = Module(new CPU)
cpu.io.interrupt_flag := Cat(uart.io.signal_interrupt, timer.io.signal_interrupt)
cpu.io.csr_regs_debug_read_address := 0.U
cpu.io.regs_debug_read_address := 0.U
cpu.io.instruction_valid := rom_loader.io.load_finished
mem.io.instruction_address := cpu.io.instruction_address
cpu.io.instruction := mem.io.instruction
when(!rom_loader.io.load_finished) {
rom_loader.io.bundle <> mem.io.bundle
cpu.io.memory_bundle.read_data := 0.U
}.otherwise {
rom_loader.io.bundle.read_data := 0.U
when(cpu.io.deviceSelect === 4.U) {
cpu.io.memory_bundle <> timer.io.bundle
}.elsewhen(cpu.io.deviceSelect === 2.U) {
cpu.io.memory_bundle <> uart.io.bundle
}.elsewhen(cpu.io.deviceSelect === 1.U) {
cpu.io.memory_bundle <> display.io.bundle
}.otherwise {
cpu.io.memory_bundle <> mem.io.bundle
}
}
}
io.led := 15.U(4.W)
display.io.x := hdmi_display.io.x
display.io.y := hdmi_display.io.y
display.io.video_on := hdmi_display.io.video_on
hdmi_display.io.rgb := display.io.rgb
io.hdmi_hpdn := 1.U
io.hdmi_data_n := hdmi_display.io.TMDSdata_n
io.hdmi_data_p := hdmi_display.io.TMDSdata_p
io.hdmi_clk_n := hdmi_display.io.TMDSclk_n
io.hdmi_clk_p := hdmi_display.io.TMDSclk_p
}
object VerilogGenerator extends App {
(new ChiselStage).execute(Array("-X", "verilog", "-td", "verilog/pynq"), Seq(ChiselGeneratorAnnotation(() => new Top)))
}

View File

@@ -0,0 +1,47 @@
// Copyright 2022 Canbin Huang
//
// 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 board.verilator
import chisel3._
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}
import peripheral._
import riscv.{CPUBundle, Parameters}
import riscv.core.CPU
class Top extends Module {
val io = IO(new CPUBundle)
val cpu = Module(new CPU)
cpu.io.regs_debug_read_address := io.regs_debug_read_address
cpu.io.csr_regs_debug_read_address := io.csr_regs_debug_read_address
io.csr_regs_debug_read_data := cpu.io.csr_regs_debug_read_data
io.regs_debug_read_data := cpu.io.regs_debug_read_data
// intercept UART signals
io.deviceSelect := cpu.io.deviceSelect
// CPU instruction input is controlled by external codes
io.memory_bundle <> cpu.io.memory_bundle
io.instruction_address := cpu.io.instruction_address
cpu.io.instruction := io.instruction
cpu.io.instruction_valid := io.instruction_valid
cpu.io.interrupt_flag := io.interrupt_flag
}
object VerilogGenerator extends App {
(new ChiselStage).execute(Array("-X", "verilog", "-td", "verilog/verilator"), Seq(ChiselGeneratorAnnotation(() =>
new Top())))
}

View File

@@ -0,0 +1,111 @@
// Copyright 2022 Canbin Huang
//
// 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 board.z710
import chisel3._
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}
import chisel3.util.Cat
import peripheral._
import riscv.Parameters
import riscv.core.{CPU, ProgramCounter}
class Top(binaryFilename: String = "say_goodbye.asmbin") extends Module {
val io = IO(new Bundle() {
val tx = Output(Bool())
val rx = Input(Bool())
val led = Output(Bool()) // z710 has few LEDs, use one for running indicator
})
// original ref clock is 125MHz, divided in clock_control.v by 5 to avoid total negative slack too large
val clock_freq = 25_000_000
val mem = Module(new Memory(Parameters.MemorySizeInWords))
// val hdmi_display = Module(new HDMIDisplay)
// val display = Module(new CharacterDisplay)
val timer = Module(new Timer)
val uart = Module(new Uart(frequency = clock_freq, baudRate = 115200))
val dummy = Module(new Dummy)
// display.io.bundle <> dummy.io.bundle
mem.io.bundle <> dummy.io.bundle
mem.io.debug_read_address := 0.U
timer.io.bundle <> dummy.io.bundle
uart.io.bundle <> dummy.io.bundle
io.tx := uart.io.txd
uart.io.rxd := io.rx
val instruction_rom = Module(new InstructionROM(binaryFilename))
val rom_loader = Module(new ROMLoader(instruction_rom.capacity))
rom_loader.io.rom_data := instruction_rom.io.data
rom_loader.io.load_address := Parameters.EntryAddress
instruction_rom.io.address := rom_loader.io.rom_address
val CPU_clkdiv = RegInit(UInt(2.W),0.U)
val CPU_tick = Wire(Bool())
val CPU_next = Wire(UInt(2.W))
CPU_next := Mux(CPU_clkdiv === 3.U, 0.U, CPU_clkdiv + 1.U)
CPU_tick := CPU_clkdiv === 0.U
CPU_clkdiv := CPU_next
withClock(CPU_tick.asClock) {
val cpu = Module(new CPU)
cpu.io.interrupt_flag := Cat(uart.io.signal_interrupt, timer.io.signal_interrupt)
cpu.io.csr_regs_debug_read_address := 0.U
cpu.io.regs_debug_read_address := 0.U
cpu.io.instruction_valid := rom_loader.io.load_finished
mem.io.instruction_address := cpu.io.instruction_address
cpu.io.instruction := mem.io.instruction
when(!rom_loader.io.load_finished) {
rom_loader.io.bundle <> mem.io.bundle
cpu.io.memory_bundle.read_data := 0.U
}.otherwise {
rom_loader.io.bundle.read_data := 0.U
when(cpu.io.deviceSelect === 4.U) {
cpu.io.memory_bundle <> timer.io.bundle
}.elsewhen(cpu.io.deviceSelect === 2.U) { // deviceSelect = highest 3 bits of address, thus 0x4000_0000 is mapped to UART
cpu.io.memory_bundle <> uart.io.bundle
}.otherwise {
cpu.io.memory_bundle <> mem.io.bundle
}
}
when (uart.io.bundle.write_enable) {
val the_char = cpu.io.memory_bundle.write_data(7, 0)
printf(cf"${the_char.asUInt}%c")
}
}
// LED, blinks every second
val led_count = RegInit(0.U(32.W))
when (led_count >= clock_freq.U) {
led_count := 0.U
}.otherwise {
led_count := led_count + 1.U
}
io.led := (led_count >= (clock_freq.U >> 1))
}
object VerilogGenerator extends App {
(new ChiselStage).execute(
Array("-X", "verilog", "-td", "verilog/z710"),
Seq(ChiselGeneratorAnnotation(() => new Top("say_goodbye.asmbin"))) // program to run on CPU
)
}

View File

@@ -0,0 +1,123 @@
// Copyright 2022 Canbin Huang
//
// 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 board.z710v1_3
import chisel3._
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}
import chisel3.util.Cat
import peripheral._
import riscv.Parameters
import riscv.core.{CPU, ProgramCounter}
class Top(binaryFilename: String = "say_goodbye.asmbin") extends Module {
val io = IO(new Bundle() {
val tx = Output(Bool())
val rx = Input(Bool())
val led = Output(Bool()) // z710 has few LEDs, use one for running indicator
})
val clock_freq = 50_000_000
val mem = Module(new Memory(Parameters.MemorySizeInWords))
// val hdmi_display = Module(new HDMIDisplay)
// val display = Module(new CharacterDisplay)
val timer = Module(new Timer)
val uart = Module(new Uart(frequency = clock_freq, baudRate = 115200))
val dummy = Module(new Dummy)
// display.io.bundle <> dummy.io.bundle
mem.io.bundle <> dummy.io.bundle
mem.io.debug_read_address := 0.U
timer.io.bundle <> dummy.io.bundle
uart.io.bundle <> dummy.io.bundle
io.tx := uart.io.txd
uart.io.rxd := io.rx
val instruction_rom = Module(new InstructionROM(binaryFilename))
val rom_loader = Module(new ROMLoader(instruction_rom.capacity))
rom_loader.io.rom_data := instruction_rom.io.data
rom_loader.io.load_address := Parameters.EntryAddress
instruction_rom.io.address := rom_loader.io.rom_address
val CPU_clkdiv = RegInit(UInt(2.W),0.U)
val CPU_tick = Wire(Bool())
val CPU_next = Wire(UInt(2.W))
CPU_next := Mux(CPU_clkdiv === 3.U, 0.U, CPU_clkdiv + 1.U)
CPU_tick := CPU_clkdiv === 0.U
CPU_clkdiv := CPU_next
withClock(CPU_tick.asClock) {
val cpu = Module(new CPU)
// cpu.io.interrupt_flag := Cat(uart.io.signal_interrupt, timer.io.signal_interrupt)
/* disable interrupt flag for now, some unexpected bugs in Zybo 7010 v1.3 board*/
cpu.io.interrupt_flag := 0.U
cpu.io.csr_regs_debug_read_address := 0.U
cpu.io.regs_debug_read_address := 0.U
// cpu.io.debug_read_address := 0.U
// cpu.io.memory_bundle.read_data := 0.U
cpu.io.instruction_valid := rom_loader.io.load_finished
mem.io.instruction_address := cpu.io.instruction_address
cpu.io.instruction := mem.io.instruction
when(!rom_loader.io.load_finished) {
rom_loader.io.bundle <> mem.io.bundle
cpu.io.memory_bundle.read_data := 0.U
}.otherwise {
rom_loader.io.bundle.read_data := 0.U
when(cpu.io.deviceSelect === 2.U) {
cpu.io.memory_bundle <> uart.io.bundle
}.otherwise {
cpu.io.memory_bundle <> mem.io.bundle
}
}
when(!rom_loader.io.load_finished) {
rom_loader.io.bundle <> mem.io.bundle
cpu.io.memory_bundle.read_data := 0.U
}.otherwise {
rom_loader.io.bundle.read_data := 0.U
cpu.io.memory_bundle <> mem.io.bundle
}
when (uart.io.bundle.write_enable) {
val the_char = cpu.io.memory_bundle.write_data(7, 0)
printf(cf"${the_char.asUInt}%c")
}
}
// LED, blinks every second
val led_count = RegInit(0.U(32.W))
when (led_count >= clock_freq.U) {
led_count := 0.U
}.otherwise {
led_count := led_count + 1.U
}
io.led := (led_count >= (clock_freq.U >> 1))
}
object VerilogGenerator extends App {
(new ChiselStage).execute(
Array("-X", "verilog", "-td", "verilog/z710v1.3"),
Seq(ChiselGeneratorAnnotation(() => new Top("say_goodbye.asmbin"))) // program to run on CPU
)
}