Files
simulation_Peripheral/TimeSlice_Timer_v2.cs
2026-03-23 10:45:40 +08:00

161 lines
5.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//============================================================================
// SimpleWriter - 带中断功能的写寄存器外设
//============================================================================
using System;
using Antmicro.Renode.Core;
using Antmicro.Renode.Logging;
using Antmicro.Renode.Peripherals.Bus;
using Antmicro.Renode.Peripherals.IRQControllers;
namespace Antmicro.Renode.Peripherals.Timers1
{
public class SimpleWriter : IDoubleWordPeripheral, IKnownSize
{
// 寄存器偏移地址
private const uint REG0_OFFSET = 0x0000;
private const uint REG1_OFFSET = 0x0004;
private const uint REG2_OFFSET = 0x0008;
private const uint REG3_OFFSET = 0x000C;
private const uint SIZE = 0x1000;
// 寄存器值
private uint reg0;
private uint reg1;
private uint reg2;
private uint reg3;
// 中断输出 - 必须为public类型为GPIO
public GPIO Interrupt { get; private set; }
public SimpleWriter(IMachine machine)
{
this.Log(LogLevel.Info, "[中断输出创建] 中断触发: REG1低8位=0x{0:X2}, 中断=高电平", currentLow8Bits);
// 创建中断输出
Interrupt = new GPIO();
this.Log(LogLevel.Info, "[中断输出创建] 中断触发: REG1低8位=0x{0:X2}, 中断=高电平", currentLow8Bits);
// 初始化寄存器
InitializeRegisters();
LogInitializationInfo();
}
public void Reset()
{
InitializeRegisters();
// 复位时清除中断
Interrupt.Set(false);
}
/// <summary>
/// 更新中断状态
/// </summary>
private void UpdateInterrupt()
{
uint currentLow8Bits = reg1 & 0xFF;
bool interruptActive = (currentLow8Bits != 0);
// 设置中断电平
Interrupt.Set(interruptActive);
if (interruptActive)
{
this.Log(LogLevel.Info, "[SimpleWriter] 中断触发: REG1低8位=0x{0:X2}, 中断=高电平", currentLow8Bits);
}
else
{
this.Log(LogLevel.Info, "[SimpleWriter] 中断清除: REG1低8位=0x00, 中断=低电平");
}
}
private void InitializeRegisters()
{
reg0 = 0xFFFE8040;
reg1 = 0x01F90040; // 低8位=0x40有1存在
reg2 = 0x00000040;
reg3 = 0x00000000;
UpdateInterrupt();
}
public uint ReadDoubleWord(long offset)
{
uint value = 0;
switch ((uint)offset)
{
case REG0_OFFSET:
value = reg0;
this.Log(LogLevel.Info, "[SimpleWriter] REG0读取: 0x{0:X8}", value);
break;
case REG1_OFFSET:
value = reg1;
this.Log(LogLevel.Info, "[SimpleWriter] REG1读取: 0x{0:X8}", value);
break;
case REG2_OFFSET:
value = reg2;
this.Log(LogLevel.Info, "[SimpleWriter] REG2读取: 0x{0:X8}", value);
break;
case REG3_OFFSET:
value = reg3;
this.Log(LogLevel.Info, "[SimpleWriter] REG3读取: 0x{0:X8}", value);
break;
default:
this.Log(LogLevel.Warning, "[SimpleWriter] 读取未定义地址: 0x{0:X8}", offset);
break;
}
return value;
}
public void WriteDoubleWord(long offset, uint value)
{
switch ((uint)offset)
{
case REG0_OFFSET:
this.Log(LogLevel.Info, "[SimpleWriter] REG0写入: 0x{0:X8}", value);
reg0 = value;
break;
case REG1_OFFSET:
this.Log(LogLevel.Info, "[SimpleWriter] REG1写入: 旧值0x{0:X8}, 新值0x{1:X8}", reg1, value);
reg1 = value;
UpdateInterrupt(); // 写入REG1后更新中断状态
break;
case REG2_OFFSET:
this.Log(LogLevel.Info, "[SimpleWriter] REG2写入: 0x{0:X8}", value);
reg2 = value;
break;
case REG3_OFFSET:
this.Log(LogLevel.Info, "[SimpleWriter] REG3写入: 0x{0:X8}", value);
reg3 = value;
break;
default:
this.Log(LogLevel.Warning, "[SimpleWriter] 写入未定义地址: 0x{0:X8} = 0x{1:X8}", offset, value);
break;
}
}
public long Size => SIZE;
private void LogInitializationInfo()
{
this.Log(LogLevel.Warning, "==========================================");
this.Log(LogLevel.Warning, "SimpleWriter 已初始化");
this.Log(LogLevel.Warning, "");
this.Log(LogLevel.Warning, "寄存器初始化值:");
this.Log(LogLevel.Warning, " REG0: 0x{0:X8}", reg0);
this.Log(LogLevel.Warning, " REG1: 0x{0:X8} (低8位=0x{1:X2})", reg1, reg1 & 0xFF);
this.Log(LogLevel.Warning, " REG2: 0x{0:X8}", reg2);
this.Log(LogLevel.Warning, " REG3: 0x{0:X8}", reg3);
this.Log(LogLevel.Warning, "");
this.Log(LogLevel.Warning, "中断功能当REG1低8位有任何1时输出高电平中断");
this.Log(LogLevel.Warning, "当前中断状态: {0}", (reg1 & 0xFF) != 0 ? "高电平" : "低电平");
this.Log(LogLevel.Warning, "==========================================");
}
}
}