MAC(Medium Access Control)层负责协调多个用户共享同一信道资源。好的MAC协议需要在公平性、吞吐率和延迟之间取得平衡。
协议栈的核心目标是可靠传输。帧同步保证数据对齐,CRC检测残留错误,ARQ处理不可纠正的错误,MAC层协调多用户接入。每一层都为可靠性提供保障,形成纵深防御体系。
更长的CRC检错能力更强但开销更大。更激进的ARQ策略吞吐率更高但可能降低可靠性。MAC层调度需要在公平性和效率之间平衡。5G NR通过HARQ和动态调度在这些权衡中找到最优解。
// mac_scheduler.v - MAC层调度器
module mac_scheduler #(
parameter NUM_UE = 16,
parameter RB_NUM = 100
)(
input wire clk, rst_n,
input wire [NUM_UE-1:0] ue_request,
input wire [7:0] ue_cqi [0:NUM_UE-1],
output reg [RB_NUM-1:0] ue_alloc [0:NUM_UE-1],
output reg sched_valid
);
// Round-robin + proportional fair scheduling
reg [$clog2(NUM_UE)-1:0] current_ue;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) current_ue <= 0;
else current_ue <= current_ue + 1;
end
endmodule
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
# Lesson 23 simulation
def simulate():
np.random.seed(42)
num_bits = 10000
snr_range = np.arange(0, 20)
ber = []
for snr_db in snr_range:
bits = np.random.randint(0, 2, num_bits)
sym = 1 - 2*bits
noise = np.sqrt(1/(2*10**(snr_db/10)))*np.random.randn(num_bits)
rx = sym + noise
dec = (rx < 0).astype(int)
ber.append(max(np.sum(bits!=dec)/num_bits, 1e-7))
plt.figure(figsize=(10,7))
plt.semilogy(snr_range, ber, 'c-o', markersize=4)
plt.xlabel('Eb/N0 (dB)'); plt.ylabel('BER')
plt.title('Lesson 23 BER Simulation')
plt.grid(True, alpha=0.3, which='both'); plt.ylim(1e-7, 1)
plt.savefig('/var/www/ttl/digital-comm/lesson23_ber.png', dpi=100, facecolor='#0f172a')
print("Done!")
simulate()
本课主题在数字通信系统中扮演关键角色。理解其设计权衡对构建高效通信系统至关重要。在实际工程中,需要在性能、复杂度和资源之间找到最优平衡点。
| 参数 | 增大效果 | 减小效果 |
|---|---|---|
| 处理精度 | 性能提升,资源增大 | 量化噪声增大 |
| 缓冲深度 | 时延增加,吞吐平稳 | 溢出风险增大 |
| 迭代次数 | 性能提升,延迟增大 | 收敛不充分 |
| 并行度 | 吞吐率提升,面积增大 | 吞吐率受限 |
在完整的通信系统中,本课模块需要与上下游模块正确对接。接口设计遵循AXI-Stream协议:tdata(数据)、tvalid(有效)、tready(就绪)、tlast(包结束)。这种握手协议保证了模块间的数据流控制,避免数据丢失。
背压(Backpressure)机制:当下游模块处理不过来时,通过拉低tready信号通知上游暂停发送。上游模块必须在tvalid&tready同时为高时才发送数据。这种机制保证了数据完整性,是流式处理的基础。
此外还需要考虑:
通信协议栈采用分层架构,每层只关注自己的功能,通过标准接口与上下层交互。这种设计使得各层可以独立升级和优化。
5G NR的协议栈相比4G增加了SDAP层(业务数据适配),支持网络切片的QoS映射。
完整的通信系统仿真需要考虑多个因素:信道模型、编码增益、同步误差、实现损耗等。以下Python代码提供了完整的系统级仿真框架。
#!/usr/bin/env python3
# 第23课系统级仿真
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import erfc
def ber_theory_bpsk(snr_db):
return 0.5 * erfc(np.sqrt(10**(snr_db/10)))
def simulate_system(mod_type='bpsk', coding_gain_db=0, num_bits=50000):
np.random.seed(42)
snr_range = np.arange(0, 20)
ber_sim = []
for snr_db in snr_range:
effective_snr = snr_db + coding_gain_db
snr_lin = 10**(effective_snr/10)
bits = np.random.randint(0, 2, num_bits)
symbols = 1 - 2*bits
noise_std = 1.0 / np.sqrt(2*snr_lin)
noise = noise_std * np.random.randn(len(symbols))
rx = symbols + noise
dec = (rx < 0).astype(int)
ber = np.sum(bits != dec) / num_bits
ber_sim.append(max(ber, 1e-7))
return snr_range, ber_sim
snr, ber_u = simulate_system('bpsk', 0)
_, ber_coded = simulate_system('bpsk', 2)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
ax1.semilogy(snr, ber_u, 'c-o', markersize=3, label='未编码')
ax1.semilogy(snr, ber_coded, '#10b981-s', markersize=3, label='编码(+2dB)')
ax1.set_xlabel('Eb/N0 (dB)'); ax1.set_ylabel('BER')
ax1.set_title('第23课:BER仿真'); ax1.legend()
ax1.grid(True, alpha=0.3, which='both'); ax1.set_ylim(1e-7, 1)
snr_range2 = np.arange(0, 25)
throughput = [(1 - ber_theory_bpsk(s)) * 1e6 for s in snr_range2]
ax2.plot(snr_range2, np.array(throughput)/1e6, '#f59e0b', linewidth=2)
ax2.set_xlabel('SNR (dB)'); ax2.set_ylabel('吞吐率 (Mbps)')
ax2.set_title('吞吐率 vs SNR'); ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('/var/www/ttl/digital-comm/lesson23_sys.png', dpi=100,
facecolor='#0f172a', edgecolor='none')
print("系统级仿真图已保存")
实际硬件实现与理论性能之间总存在差距,称为实现损耗(Implementation Loss)。主要来源:
典型总实现损耗:3-6dB。好的设计可以将损耗控制在3dB以内。
在高吞吐率通信系统中,时序优化至关重要。常用的优化技术包括:
| 模块 | LUT | FF | BRAM | DSP | 频率 |
|---|---|---|---|---|---|
| BCH编码器 | 200 | 50 | 0 | 0 | 350MHz |
| Viterbi解码器 | 5000 | 2000 | 4 | 0 | 200MHz |
| LDPC解码器 | 20000 | 8000 | 20 | 0 | 250MHz |
| 64点FFT | 3000 | 1500 | 2 | 8 | 300MHz |
| OFDM调制器 | 5000 | 2500 | 4 | 12 | 250MHz |
| Costas环 | 1500 | 800 | 2 | 4 | 200MHz |
以上为Xilinx Zynq UltraScale+器件上的典型资源估计。实际资源取决于具体参数配置。
通信模块的验证采用"双重参考模型"方法:
对于本课模块,关键验证点包括:边界输入、溢出条件、复位行为、背压处理等。
练习1:实现MAC层设计:多用户接入控制的完整Verilog模块,通过仿真验证功能。
练习2:用Python仿真MAC层设计:多用户接入控制在不同SNR下的BER性能。
练习3:分析MAC层设计:多用户接入控制的参数变化对系统性能的影响。
练习4:优化MAC层设计:多用户接入控制的硬件实现,减少资源占用。
练习5:将MAC层设计:多用户接入控制集成到完整的通信系统中测试。
你掌握了MAC层!
下一课预告:第24课整合协议栈。