Digital Logic: Integrated Project III—System Integration and Debugging
Objectives
- Write the final top-level module,
stopwatch_top, integrating all submodules. - Write a complete XDC pin-constraint file.
- Master basic methods for on-board debugging.
- Complete on-board verification of the stopwatch system.
1. Top-Level Module Design
1.1 Port Definitions
The ports of the top-level module correspond directly to physical FPGA pins:
module stopwatch_top (
input clk, // 100MHz 系统时钟
input sys_rst_n, // 系统复位按键(低有效)
input btn_ss, // Start/Stop 按键
input btn_clr, // Clear/Reset 按键
output [7:0] seg_r, // 右侧数码管段选 {a,b,c,d,e,f,g,dp}
output [7:0] seg_l, // 左侧数码管段选(未使用,全灭)
output [7:0] an, // 数码管位选 {BIT8,...,BIT1}
output [2:0] led_state // LED 指示当前状态
);
1.2 Internal Signals
// 控制信号
wire running, clear;
wire counter_rst_n;
wire tick_100hz;
// 按键信号
wire btn_ss_clean, btn_ss_pulse;
wire btn_clr_clean, btn_clr_pulse;
// 计数器信号
wire [3:0] cnt0, cnt1, cnt2, cnt3;
wire carry0, carry1, carry2;
// 显示信号
wire [3:0] an_active;
wire [7:0] seg_active;
assign counter_rst_n = sys_rst_n & ~clear;
assign seg_l = 8'b0; // 左侧数码管全灭
assign an = {4'b0000, an_active}; // 只用右侧4位
1.3 Module Instantiation
// ==== 分频器 ====
clk_div #(.MAX_COUNT(1_000_000)) u_100hz (
.clk(clk), .rst_n(counter_rst_n), .tick(tick_100hz)
);
// ==== 按键处理 ====
btn_debounce u_deb_ss (
.clk(clk), .rst_n(sys_rst_n), .btn_in(btn_ss), .btn_out(btn_ss_clean));
edge_detect u_edge_ss (
.clk(clk), .rst_n(sys_rst_n), .sig_in(btn_ss_clean),
.pos_edge(btn_ss_pulse), .neg_edge());
btn_debounce u_deb_clr (
.clk(clk), .rst_n(sys_rst_n), .btn_in(btn_clr), .btn_out(btn_clr_clean));
edge_detect u_edge_clr (
.clk(clk), .rst_n(sys_rst_n), .sig_in(btn_clr_clean),
.pos_edge(btn_clr_pulse), .neg_edge());
// ==== 状态机 ====
stopwatch_ctrl u_ctrl (
.clk(clk), .rst_n(sys_rst_n),
.btn_ss(btn_ss_pulse), .btn_reset(btn_clr_pulse),
.running(running), .clear(clear)
);
// ==== 计数器链 ====
bcd_counter u_cs0 (.clk(clk), .rst_n(counter_rst_n),
.en(tick_100hz & running), .cnt(cnt0), .carry(carry0));
bcd_counter u_cs1 (.clk(clk), .rst_n(counter_rst_n),
.en(carry0), .cnt(cnt1), .carry(carry1));
bcd_counter u_s0 (.clk(clk), .rst_n(counter_rst_n),
.en(carry1), .cnt(cnt2), .carry(carry2));
bcd_counter u_s1 (.clk(clk), .rst_n(counter_rst_n),
.en(carry2), .cnt(cnt3), .carry());
// ==== 显示 ====
dynamic_display u_disp (
.clk(clk), .rst_n(sys_rst_n),
.digit3(cnt3), .digit2(cnt2),
.digit1(cnt1), .digit0(cnt0),
.dp_in(4'b0100), // 小数点在秒与百分秒之间
.seg(seg_r), .an(an_active)
);
// ==== 状态LED指示 ====
assign led_state = {
(u_ctrl.state == 2'd2), // LED[2]: PAUSED
(u_ctrl.state == 2'd1), // LED[1]: RUNNING
(u_ctrl.state == 2'd0) // LED[0]: IDLE
};
Note:
led_stateabove uses the hierarchical referenceu_ctrl.state, which is valid in simulation. If synthesis does not support it, exposestateas an output port ofstopwatch_ctrl.
2. Pin Constraints (XDC)
2.1 Complete Constraint File
## ============================================================
## 秒表项目 - EGO1 引脚约束
## ============================================================
## 系统时钟 100MHz
set_property PACKAGE_PIN P17 [get_ports clk]
set_property IOSTANDARD LVCMOS33 [get_ports clk]
## 按键(低有效)
set_property PACKAGE_PIN P15 [get_ports sys_rst_n]
set_property IOSTANDARD LVCMOS33 [get_ports sys_rst_n]
set_property PACKAGE_PIN R15 [get_ports btn_ss]
set_property IOSTANDARD LVCMOS33 [get_ports btn_ss]
set_property PACKAGE_PIN R11 [get_ports btn_clr]
set_property IOSTANDARD LVCMOS33 [get_ports btn_clr]
## 右侧数码管段选 seg_r = {a, b, c, d, e, f, g, dp}
set_property PACKAGE_PIN B4 [get_ports {seg_r[7]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_r[7]}]
set_property PACKAGE_PIN A4 [get_ports {seg_r[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_r[6]}]
set_property PACKAGE_PIN A3 [get_ports {seg_r[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_r[5]}]
set_property PACKAGE_PIN B1 [get_ports {seg_r[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_r[4]}]
set_property PACKAGE_PIN A1 [get_ports {seg_r[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_r[3]}]
set_property PACKAGE_PIN B3 [get_ports {seg_r[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_r[2]}]
set_property PACKAGE_PIN B2 [get_ports {seg_r[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_r[1]}]
set_property PACKAGE_PIN D5 [get_ports {seg_r[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_r[0]}]
## 左侧数码管段选(全灭,但仍需约束避免警告)
set_property PACKAGE_PIN D4 [get_ports {seg_l[7]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_l[7]}]
set_property PACKAGE_PIN E3 [get_ports {seg_l[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_l[6]}]
set_property PACKAGE_PIN D3 [get_ports {seg_l[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_l[5]}]
set_property PACKAGE_PIN F4 [get_ports {seg_l[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_l[4]}]
set_property PACKAGE_PIN F3 [get_ports {seg_l[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_l[3]}]
set_property PACKAGE_PIN E2 [get_ports {seg_l[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_l[2]}]
set_property PACKAGE_PIN D2 [get_ports {seg_l[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_l[1]}]
set_property PACKAGE_PIN H2 [get_ports {seg_l[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {seg_l[0]}]
## 数码管位选 an = {BIT8, BIT7, ..., BIT1}
set_property PACKAGE_PIN G6 [get_ports {an[7]}]
set_property IOSTANDARD LVCMOS33 [get_ports {an[7]}]
set_property PACKAGE_PIN E1 [get_ports {an[6]}]
set_property IOSTANDARD LVCMOS33 [get_ports {an[6]}]
set_property PACKAGE_PIN F1 [get_ports {an[5]}]
set_property IOSTANDARD LVCMOS33 [get_ports {an[5]}]
set_property PACKAGE_PIN G1 [get_ports {an[4]}]
set_property IOSTANDARD LVCMOS33 [get_ports {an[4]}]
set_property PACKAGE_PIN H1 [get_ports {an[3]}]
set_property IOSTANDARD LVCMOS33 [get_ports {an[3]}]
set_property PACKAGE_PIN C1 [get_ports {an[2]}]
set_property IOSTANDARD LVCMOS33 [get_ports {an[2]}]
set_property PACKAGE_PIN C2 [get_ports {an[1]}]
set_property IOSTANDARD LVCMOS33 [get_ports {an[1]}]
set_property PACKAGE_PIN G2 [get_ports {an[0]}]
set_property IOSTANDARD LVCMOS33 [get_ports {an[0]}]
## LED 状态指示(根据手册确认引脚)
# set_property PACKAGE_PIN ?? [get_ports {led_state[2]}]
# set_property IOSTANDARD LVCMOS33 [get_ports {led_state[2]}]
# set_property PACKAGE_PIN ?? [get_ports {led_state[1]}]
# set_property IOSTANDARD LVCMOS33 [get_ports {led_state[1]}]
# set_property PACKAGE_PIN ?? [get_ports {led_state[0]}]
# set_property IOSTANDARD LVCMOS33 [get_ports {led_state[0]}]
Important: The EGO1 buttons are active-low, and
sys_rst_nis designed as an active-low reset. Fill in the LED pins by consulting theEGo1.xdcreference file. The seven-segment display pins shown above have been confirmed.
3. On-Board Debugging
3.1 Incremental Verification Strategy
Do not begin by testing the entire system. Verify it incrementally in the following order:
| Step | Item under test | Method |
|---|---|---|
| 1 | Clock divider | Connect its 1 Hz output to an LED and confirm the blink rate |
| 2 | Seven-segment display | Apply a fixed input of 1234 and confirm the display |
| 3 | Counters | Let the counters run continuously and confirm that the displayed value increments |
| 4 | Buttons | Use a button to control one LED and confirm that debouncing is effective |
| 5 | Complete system | Integrate and test all functions together |
3.2 Troubleshooting Common Problems
| Symptom | Possible cause | Diagnostic method |
|---|---|---|
| Display is blank | Incorrect digit- or segment-select pins | Test a static display first |
| Display shows incorrect glyphs | Incorrect segment-code bit order | Apply fixed inputs 0–9 and check each one |
| Button does not respond | Incorrect pin or reversed active level | Connect the button directly to an LED for testing |
| Timing is inaccurate | Incorrect division factor | Compare against a stopwatch on a phone |
| One press changes multiple counts | Debounce parameter is too small | Increase DEBOUNCE_MAX |
| Clear does not work | Incorrect FSM logic | Inspect state transitions in simulation |
3.3 Troubleshooting with Simulation Waveforms
If the board behaves unexpectedly, return to simulation:
- Reproduce the same input sequence in the testbench.
- Inspect internal waveforms, including state, enable, and carry signals.
- Find the first signal that differs from the expected behavior and identify the responsible module.
4. Laboratory Tasks for This Week
Step 1: Write stopwatch_top.v
Integrate all modules using the code in Section 1 as a reference.
Step 2: Write the XDC Constraints
Copy the constraints from Section 2 and fill in the correct button and LED pins.
Step 3: Simulate the Complete System
Write a testbench that performs the following sequence:
- Reset.
- Press Start and observe counting begin.
- Wait several seconds.
- Press Stop and observe counting stop.
- Press Start and observe counting resume.
- Press Stop to pause.
- Press Reset and observe the display clear.
Step 4: Verify on the Board
Use the incremental strategy from Section 3.1 to verify each function.
On-Board Acceptance Criteria
- The display reads
00.00after power-up. - Pressing Start begins timing and the value increments rapidly.
- Pressing Stop pauses timing and freezes the displayed value.
- Pressing Start while paused resumes timing.
- Pressing Reset while paused clears the display to
00.00. - Pressing Reset while running has no effect and does not clear the count.
- The display is stable and the decimal point is correct.

