File size: 6,334 Bytes
806d4b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
U0 PortNop()
{//Innoculous (reads IRQ Mask) which should take fixed time
//because it's an ISA-bus standard. It takes 1.0uS-2.0uS.
InU8(0x21);
}
U16 EndianU16(U16 d)
{//Swap big<-->little endian.
I64 res=0;
res.u8[1]=d.u8[0];
res.u8[0]=d.u8[1];
return res;
}
U32 EndianU32(U32 d)
{//Swap big<-->little endian.
I64 res=0;
res.u8[3]=d.u8[0];
res.u8[2]=d.u8[1];
res.u8[1]=d.u8[2];
res.u8[0]=d.u8[3];
return res;
}
I64 EndianI64(I64 d)
{//Swap big<-->little endian.
U64 res;
res.u8[7]=d.u8[0];
res.u8[6]=d.u8[1];
res.u8[5]=d.u8[2];
res.u8[4]=d.u8[3];
res.u8[3]=d.u8[4];
res.u8[2]=d.u8[5];
res.u8[1]=d.u8[6];
res.u8[0]=d.u8[7];
return res;
}
I64 BCnt(I64 d)
{//Count set bits in I64.
I64 res=0,i;
for (i=0;i<8;i++)
res+=set_bits_table[d.u8[i]];
return res;
}
U0 IntCore0TimerHndlr(CTask *)
{//Called from IntCore0TimerHndlr
I64 i;
if (mp_cnt>1)
while (LBts(&sys_semas[SEMA_SYS_TIMER],0))
PAUSE
lock cnts.jiffies++;
cnts.timer+=SYS_TIMER0_PERIOD+1;
LBtr(&sys_semas[SEMA_SYS_TIMER],0);
for (i=1;i<mp_cnt;i++)
MPInt(I_TIMER,i);
OutU8(0x20,0x20); //Acknowledge PIC Interrupt
}
I64 SysTimerRead()
{//System timer count with overflow already handled.
I64 i,res;
PUSHFD
CLI
if (mp_cnt>1)
while (LBts(&sys_semas[SEMA_SYS_TIMER],0))
PAUSE
OutU8(0x43,0); //Latch Timer0
if ((i=InU8(0x40)+InU8(0x40)<<8)==SYS_TIMER0_PERIOD) {
if (InU8(0x20) & 1)
i=-1;
}
res=cnts.timer+SYS_TIMER0_PERIOD-i;
LBtr(&sys_semas[SEMA_SYS_TIMER],0);
POPFD
return res;
}
I64 HPET()
{ //Get high precision event timer.
return *(dev.uncached_alias+HPET_MAIN_CNT)(I64 *);
}
I64 TimeStampFreqCal()
{
static I64 time_stamp_start=0,timer_start=0,HPET_start=0;
I64 i;
if (time_stamp_start) {
PUSHFD
CLI
if (HPET_start) {
cnts.time_stamp_freq=cnts.HPET_freq*(GetTSC-time_stamp_start);
i=HPET-HPET_start;
} else {
cnts.time_stamp_freq=SYS_TIMER_FREQ*(GetTSC-time_stamp_start);
i=SysTimerRead-timer_start;
}
if (!i)
AdamErr("Timer Cal Error");
else {
cnts.time_stamp_freq/=i;
cnts.time_stamp_kHz_freq=cnts.time_stamp_freq/1000;
cnts.time_stamp_calibrated=TRUE;
}
POPFD
}
PUSHFD
CLI
if (cnts.HPET_freq) {
timer_start=0;
HPET_start=HPET;
} else {
timer_start=SysTimerRead;
HPET_start=0;
}
time_stamp_start=GetTSC;
POPFD
return cnts.time_stamp_freq;
}
F64 tS()
{//Time since boot in seconds as a float.
if (cnts.HPET_freq)
return ToF64(HPET-cnts.HPET_initial)/cnts.HPET_freq;
else
return SysTimerRead/ToF64(SYS_TIMER_FREQ);
}
Bool Blink(F64 Hz=2.5)
{//Return TRUE, then FALSE, then TRUE at given frequency.
if (!Hz) return 0;
return ToI64(cnts.jiffies*2*Hz/JIFFY_FREQ)&1;
}
U0 Busy(I64 uS)
{//Loosely timed.
I64 i;
if (cnts.HPET_freq) {
i=HPET+cnts.HPET_freq*uS/1000000;
while (HPET<i);
} else
for (i=0;i<uS;i++)
PortNop;
}
U0 SleepUntil(I64 wake_jiffy)
{//Not for power-saving. It is to make a program pause without hogging the CPU.
Bool old_idle=LBts(&Fs->task_flags,TASKf_IDLE);
Fs->wake_jiffy=wake_jiffy;
Yield;
LBEqu(&Fs->task_flags,TASKf_IDLE,old_idle);
}
U0 Sleep(I64 mS)
{//Not for power-saving. It is to make a program pause without hogging the CPU.
if (!mS)
Yield;
else
SleepUntil(cnts.jiffies+mS*JIFFY_FREQ/1000);
}
F64 Ona2Freq(I8 ona)
{//Ona to freq. Ona=60 is 440.0Hz.
if (ona==ONA_REST)
return 0;
else
return 440.0/32*2.0`(ona/12.0);
}
I8 Freq2Ona(F64 freq)
{//Freq to Ona. 440.0Hz is Ona=60.
if (freq>0)
return ClampI64(12*Log2(32.0/440.0*freq),ONA_REST+1,I8_MAX);
else
return ONA_REST;
}
U0 Snd(I8 ona=ONA_REST)
{//Play ona, a piano key num. -128 means rest.
I64 period;
CSndData *d;
if (!Bt(&sys_semas[SEMA_MUTE],0) &&
!LBts(&sys_semas[SEMA_SND],0)) { //Mutex. Just throw-out if in use
if (ona<=ONA_REST) {
snd.ona=ona;
OutU8(0x61,InU8(0x61)&~3);
} else if (ona!=snd.ona) {
snd.ona=ona;
period=ClampI64(SYS_TIMER_FREQ/Ona2Freq(ona),1,U16_MAX);
OutU8(0x43,0xB6);
OutU8(0x42,period);
OutU8(0x42,period.u8[1]);
OutU8(0x61,3|InU8(0x61));
}
if (!IsDbgMode && snd.record) {
d=ACAlloc(sizeof(CSndData));
d->ona=ona;
d->time=tS;
QueIns(d,snd.record_head.last);
}
LBtr(&sys_semas[SEMA_SND],0);
}
}
U0 SndRst()
{//Fix stuck sound.
if (Bt(&sys_semas[SEMA_SND],0)) {
Sleep(1);
if (Bt(&sys_semas[SEMA_SND],0)) {
Sleep(1);
LBtr(&sys_semas[SEMA_SND],0);
}
}
Snd;
}
U0 Beep(I8 ona=62,Bool busy=FALSE)
{//Make beep at given ona freq.
Snd(ona);
if (busy)
Busy(500000);
else
Sleep(500);
Snd;
if (busy)
Busy(200000);
else
Sleep(200);
}
Bool Mute(Bool val)
{//Turn-off sound.
Bool res;
if (val) {
PUSHFD
CLI
Snd;
res=LBts(&sys_semas[SEMA_MUTE],0);
POPFD
} else
res=LBtr(&sys_semas[SEMA_MUTE],0);
return res;
}
Bool IsMute()
{//Return is-mute flag.
return Bt(&sys_semas[SEMA_MUTE],0);
}
Bool Silent(Bool val=ON)
{//Turn-off StdOut console text. (Not sound.)
return LBEqu(&Fs->display_flags,DISPLAYf_SILENT,val);
}
Bool IsSilent()
{//Return StdOut turned-off?
return Bt(&Fs->display_flags,DISPLAYf_SILENT);
}
Bool SysDbg(Bool val)
{//Set SysDbg bit you can use while debugging.
return LBEqu(&sys_semas[SEMA_DEBUG],0,val);
}
Bool IsSysDbg()
{//Return SysDbg bit.
return Bt(&sys_semas[SEMA_DEBUG],0);
}
Bool Raw(Bool val)
{//Set to direct scrn, BLACK & WHITE, non-windowed output mode.
if (!val)
VGAFlush;
return !LBEqu(&Fs->display_flags,DISPLAYf_NOT_RAW,!val);
}
Bool IsRaw()
{//Are we in BLACK & WHITE raw scrn mode?
return !Bt(&Fs->display_flags,DISPLAYf_NOT_RAW);
}
Bool SingleUser(Bool val)
{//Set single-user mode.
return LBEqu(&sys_semas[SEMA_SINGLE_USER],0,val);
}
Bool IsSingleUser()
{//Return single-user mode.
return Bt(&sys_semas[SEMA_SINGLE_USER],0);
}
Bool DbgMode(Bool val)
{//Set dbg-mode.
return LBEqu(&sys_semas[SEMA_DBG_MODE],0,val);
}
Bool IsDbgMode()
{//Return dbg-mode.
return Bt(&sys_semas[SEMA_DBG_MODE],0);
}
U0 ProgressBarsRst(U8 *path=NULL)
{//Reset all progress bars to zero.
CallExtStr("ProgressBarsRegTf",path);
MemSet(sys_progresses,0,sizeof(sys_progresses));
}
|