@@ -83,10 +83,14 @@ typedef struct RISCVMachine {
83
83
#define RTC_FREQ_DIV 16 /* arbitrary, relative to CPU freq to have a
84
84
10 MHz frequency */
85
85
86
+ void set_timecmp (RISCVMachine * machine0 , uint64_t timecmp );
87
+ void print_console (RISCVMachine * machine0 , const char * buf , int len );
88
+
86
89
uint64_t ecall_addr = 0 ;
87
90
uint64_t rdtime_addr = 0 ;
88
91
uint64_t dcache_iall_addr = 0 ;
89
92
uint64_t sync_s_addr = 0 ;
93
+ uint64_t real_time = 0 ;
90
94
91
95
static uint64_t rtc_get_real_time (RISCVMachine * s )
92
96
{
@@ -1042,7 +1046,6 @@ static VirtMachine *riscv_machine_init(const VirtMachineParams *p)
1042
1046
if (p -> console ) {
1043
1047
//// Begin Test: Save the Console
1044
1048
const char * msg = "TinyEMU Emulator for Ox64 BL808 RISC-V SBC\r\n" ;
1045
- void print_console (RISCVMachine * machine0 , const char * buf , int len );
1046
1049
print_console (s , msg , strlen (msg ));
1047
1050
//// End Test
1048
1051
vbus -> irq = & s -> plic_irq [irq_num ];
@@ -1131,6 +1134,7 @@ static VirtMachine *riscv_machine_init(const VirtMachineParams *p)
1131
1134
p -> cmdline ,
1132
1135
p -> files [VM_FILE_INITRD ].buf , p -> files [VM_FILE_INITRD ].len );
1133
1136
1137
+ set_timecmp (s , 0 ); //// Init the System Timer
1134
1138
return (VirtMachine * )s ;
1135
1139
}
1136
1140
@@ -1167,10 +1171,16 @@ static int riscv_machine_get_sleep_duration(VirtMachine *s1, int delay)
1167
1171
delay = 0 ;
1168
1172
1169
1173
//// Begin Test: Trigger the Supervisor-Mode Timer Interrupt
1170
- static uint64_t t = 0 ;
1171
- if (t ++ % 100 == 0 ) {
1172
- riscv_cpu_set_mip (s , MIP_STIP );
1173
- } ////
1174
+ real_time = rtc_get_time (m );
1175
+ if (!(riscv_cpu_get_mip (s ) & MIP_STIP )) {
1176
+ const int64_t delay2 = m -> timecmp - rtc_get_time (m );
1177
+ if (delay2 <= 0 ) {
1178
+ static uint64_t t = 0 ;
1179
+ if (t ++ % 100 == 0 ) {
1180
+ riscv_cpu_set_mip (s , MIP_STIP );
1181
+ }
1182
+ }
1183
+ }
1174
1184
//// End Test
1175
1185
return delay ;
1176
1186
}
@@ -1216,7 +1226,17 @@ const VirtMachineClass riscv_machine_class = {
1216
1226
riscv_vm_send_key_event ,
1217
1227
};
1218
1228
1219
- //// Begin Test: Print to Console
1229
+ //// Begin Test
1230
+ // Set Timer
1231
+ void set_timecmp (RISCVMachine * machine0 , uint64_t timecmp ) {
1232
+ static RISCVMachine * machine = NULL ;
1233
+ if (machine0 != NULL ) { machine = machine0 ; return ; }
1234
+ if (machine == NULL ) { puts ("set_timecmp: machine is null" ); return ; }
1235
+ machine -> timecmp = timecmp ;
1236
+ printf ("set_timecmp: timecmp=%p\n" , timecmp );
1237
+ }
1238
+
1239
+ // Print to Console
1220
1240
void print_console (RISCVMachine * machine0 , const char * buf , int len ) {
1221
1241
static RISCVMachine * machine = NULL ;
1222
1242
if (machine0 != NULL ) { machine = machine0 ; }
0 commit comments