| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: cmos.cpp,v 1.29 2009-06-16 18:19:18 qbix79 Exp $ */ |
| 20 | |
| 21 | #include <time.h> |
| 22 | #include <math.h> |
| 23 | |
| 24 | #include "dosbox.h" |
| 25 | #include "timer.h" |
| 26 | #include "pic.h" |
| 27 | #include "inout.h" |
| 28 | #include "mem.h" |
| 29 | #include "bios_disk.h" |
| 30 | #include "setup.h" |
| 31 | #include "cross.h" //fmod on certain platforms |
| 32 | |
| 33 | static struct { |
| 34 | Bit8u regs[0x40]; |
| 35 | bool nmi; |
| 36 | bool bcd; |
| 37 | Bit8u reg; |
| 38 | struct { |
| 39 | bool enabled; |
| 40 | Bit8u div; |
| 41 | float delay; |
| 42 | bool acknowledged; |
| 43 | } timer; |
| 44 | struct { |
| 45 | double timer; |
| 46 | double ended; |
| 47 | double alarm; |
| 48 | } last; |
| 49 | bool update_ended; |
| 50 | } cmos; |
| 51 | |
| 52 | static void cmos_timerevent(Bitu val) { |
| 53 | if (cmos.timer.acknowledged) { |
| 54 | cmos.timer.acknowledged=false; |
| 55 | PIC_ActivateIRQ(8); |
| 56 | } |
| 57 | if (cmos.timer.enabled) { |
| 58 | PIC_AddEvent(cmos_timerevent,cmos.timer.delay); |
| 59 | cmos.regs[0xc] = 0xC0;//Contraption Zack (music) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static void cmos_checktimer(void) { |
| 64 | PIC_RemoveEvents(cmos_timerevent); |
| 65 | if (cmos.timer.div<=2) cmos.timer.div+=7; |
| 66 | cmos.timer.delay=(1000.0f/(32768.0f / (1 << (cmos.timer.div - 1)))); |
| 67 | if (!cmos.timer.div || !cmos.timer.enabled) return; |
| 68 | LOG(LOG_PIT,LOG_NORMAL)("RTC Timer at %.2f hz",1000.0/cmos.timer.delay); |
| 69 | // PIC_AddEvent(cmos_timerevent,cmos.timer.delay); |
| 70 | /* A rtc is always running */ |
| 71 | double remd=fmod(PIC_FullIndex(),(double)cmos.timer.delay); |
| 72 | PIC_AddEvent(cmos_timerevent,(float)((double)cmos.timer.delay-remd)); //Should be more like a real pc. Check |
| 73 | // status reg A reading with this (and with other delays actually) |
| 74 | } |
| 75 | |
| 76 | void cmos_selreg(Bitu port,Bitu val,Bitu iolen) { |
| 77 | cmos.reg=val & 0x3f; |
| 78 | cmos.nmi=(val & 0x80)>0; |
| 79 | } |
| 80 | |
| 81 | static void cmos_writereg(Bitu port,Bitu val,Bitu iolen) { |
| 82 | switch (cmos.reg) { |
| 83 | case 0x00: /* Seconds */ |
| 84 | case 0x02: /* Minutes */ |
| 85 | case 0x04: /* Hours */ |
| 86 | case 0x06: /* Day of week */ |
| 87 | case 0x07: /* Date of month */ |
| 88 | case 0x08: /* Month */ |
| 89 | case 0x09: /* Year */ |
| 90 | case 0x32: /* Century */ |
| 91 | /* Ignore writes to change alarm */ |
| 92 | break; |
| 93 | case 0x01: /* Seconds Alarm */ |
| 94 | case 0x03: /* Minutes Alarm */ |
| 95 | case 0x05: /* Hours Alarm */ |
| 96 | LOG(LOG_BIOS,LOG_NORMAL)("CMOS:Trying to set alarm"); |
| 97 | cmos.regs[cmos.reg]=val; |
| 98 | break; |
| 99 | case 0x0a: /* Status reg A */ |
| 100 | cmos.regs[cmos.reg]=val & 0x7f; |
| 101 | if ((val & 0x70)!=0x20) LOG(LOG_BIOS,LOG_ERROR)("CMOS Illegal 22 stage divider value"); |
| 102 | cmos.timer.div=(val & 0xf); |
| 103 | cmos_checktimer(); |
| 104 | break; |
| 105 | case 0x0b: /* Status reg B */ |
| 106 | cmos.bcd=!(val & 0x4); |
| 107 | cmos.regs[cmos.reg]=val & 0x7f; |
| 108 | cmos.timer.enabled=(val & 0x40)>0; |
| 109 | if (val&0x10) LOG(LOG_BIOS,LOG_ERROR)("CMOS:Updated ended interrupt not supported yet"); |
| 110 | cmos_checktimer(); |
| 111 | break; |
| 112 | case 0x0d:/* Status reg D */ |
| 113 | cmos.regs[cmos.reg]=val & 0x80; /*Bit 7=1:RTC Pown on*/ |
| 114 | break; |
| 115 | case 0x0f: /* Shutdown status byte */ |
| 116 | cmos.regs[cmos.reg]=val & 0x7f; |
| 117 | break; |
| 118 | default: |
| 119 | cmos.regs[cmos.reg]=val & 0x7f; |
| 120 | LOG(LOG_BIOS,LOG_ERROR)("CMOS:WRite to unhandled register %x",cmos.reg); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | #define MAKE_RETURN(_VAL) (cmos.bcd ? ((((_VAL) / 10) << 4) | ((_VAL) % 10)) : (_VAL)); |
| 126 | |
| 127 | static Bitu cmos_readreg(Bitu port,Bitu iolen) { |
| 128 | if (cmos.reg>0x3f) { |
| 129 | LOG(LOG_BIOS,LOG_ERROR)("CMOS:Read from illegal register %x",cmos.reg); |
| 130 | return 0xff; |
| 131 | } |
| 132 | Bitu drive_a, drive_b; |
| 133 | Bit8u hdparm; |
| 134 | time_t curtime; |
| 135 | struct tm *loctime; |
| 136 | /* Get the current time. */ |
| 137 | curtime = time (NULL); |
| 138 | |
| 139 | /* Convert it to local time representation. */ |
| 140 | loctime = localtime (&curtime); |
| 141 | |
| 142 | switch (cmos.reg) { |
| 143 | case 0x00: /* Seconds */ |
| 144 | return MAKE_RETURN(loctime->tm_sec); |
| 145 | case 0x02: /* Minutes */ |
| 146 | return MAKE_RETURN(loctime->tm_min); |
| 147 | case 0x04: /* Hours */ |
| 148 | return MAKE_RETURN(loctime->tm_hour); |
| 149 | case 0x06: /* Day of week */ |
| 150 | return MAKE_RETURN(loctime->tm_wday + 1); |
| 151 | case 0x07: /* Date of month */ |
| 152 | return MAKE_RETURN(loctime->tm_mday); |
| 153 | case 0x08: /* Month */ |
| 154 | return MAKE_RETURN(loctime->tm_mon + 1); |
| 155 | case 0x09: /* Year */ |
| 156 | return MAKE_RETURN(loctime->tm_year % 100); |
| 157 | case 0x32: /* Century */ |
| 158 | return MAKE_RETURN(loctime->tm_year / 100 + 19); |
| 159 | case 0x01: /* Seconds Alarm */ |
| 160 | case 0x03: /* Minutes Alarm */ |
| 161 | case 0x05: /* Hours Alarm */ |
| 162 | return cmos.regs[cmos.reg]; |
| 163 | case 0x0a: /* Status register A */ |
| 164 | if (PIC_TickIndex()<0.002) { |
| 165 | return (cmos.regs[0x0a]&0x7f) | 0x80; |
| 166 | } else { |
| 167 | return (cmos.regs[0x0a]&0x7f); |
| 168 | } |
| 169 | case 0x0c: /* Status register C */ |
| 170 | cmos.timer.acknowledged=true; |
| 171 | if (cmos.timer.enabled) { |
| 172 | /* In periodic interrupt mode only care for those flags */ |
| 173 | Bit8u val=cmos.regs[0xc]; |
| 174 | cmos.regs[0xc]=0; |
| 175 | return val; |
| 176 | } else { |
| 177 | /* Give correct values at certain times */ |
| 178 | Bit8u val=0; |
| 179 | double index=PIC_FullIndex(); |
| 180 | if (index>=(cmos.last.timer+cmos.timer.delay)) { |
| 181 | cmos.last.timer=index; |
| 182 | val|=0x40; |
| 183 | } |
| 184 | if (index>=(cmos.last.ended+1000)) { |
| 185 | cmos.last.ended=index; |
| 186 | val|=0x10; |
| 187 | } |
| 188 | return val; |
| 189 | } |
| 190 | case 0x10: /* Floppy size */ |
| 191 | drive_a = 0; |
| 192 | drive_b = 0; |
| 193 | if(imageDiskList[0] != NULL) drive_a = imageDiskList[0]->GetBiosType(); |
| 194 | if(imageDiskList[1] != NULL) drive_b = imageDiskList[1]->GetBiosType(); |
| 195 | return ((drive_a << 4) | (drive_b)); |
| 196 | /* First harddrive info */ |
| 197 | case 0x12: |
| 198 | hdparm = 0; |
| 199 | if(imageDiskList[2] != NULL) hdparm |= 0xf; |
| 200 | if(imageDiskList[3] != NULL) hdparm |= 0xf0; |
| 201 | return hdparm; |
| 202 | case 0x19: |
| 203 | if(imageDiskList[2] != NULL) return 47; /* User defined type */ |
| 204 | return 0; |
| 205 | case 0x1b: |
| 206 | if(imageDiskList[2] != NULL) return (imageDiskList[2]->cylinders & 0xff); |
| 207 | return 0; |
| 208 | case 0x1c: |
| 209 | if(imageDiskList[2] != NULL) return ((imageDiskList[2]->cylinders & 0xff00)>>8); |
| 210 | return 0; |
| 211 | case 0x1d: |
| 212 | if(imageDiskList[2] != NULL) return (imageDiskList[2]->heads); |
| 213 | return 0; |
| 214 | case 0x1e: |
| 215 | if(imageDiskList[2] != NULL) return 0xff; |
| 216 | return 0; |
| 217 | case 0x1f: |
| 218 | if(imageDiskList[2] != NULL) return 0xff; |
| 219 | return 0; |
| 220 | case 0x20: |
| 221 | if(imageDiskList[2] != NULL) return (0xc0 | (((imageDiskList[2]->heads) > 8) << 3)); |
| 222 | return 0; |
| 223 | case 0x21: |
| 224 | if(imageDiskList[2] != NULL) return (imageDiskList[2]->cylinders & 0xff); |
| 225 | return 0; |
| 226 | case 0x22: |
| 227 | if(imageDiskList[2] != NULL) return ((imageDiskList[2]->cylinders & 0xff00)>>8); |
| 228 | return 0; |
| 229 | case 0x23: |
| 230 | if(imageDiskList[2] != NULL) return (imageDiskList[2]->sectors); |
| 231 | return 0; |
| 232 | /* Second harddrive info */ |
| 233 | case 0x1a: |
| 234 | if(imageDiskList[3] != NULL) return 47; /* User defined type */ |
| 235 | return 0; |
| 236 | case 0x24: |
| 237 | if(imageDiskList[3] != NULL) return (imageDiskList[3]->cylinders & 0xff); |
| 238 | return 0; |
| 239 | case 0x25: |
| 240 | if(imageDiskList[3] != NULL) return ((imageDiskList[3]->cylinders & 0xff00)>>8); |
| 241 | return 0; |
| 242 | case 0x26: |
| 243 | if(imageDiskList[3] != NULL) return (imageDiskList[3]->heads); |
| 244 | return 0; |
| 245 | case 0x27: |
| 246 | if(imageDiskList[3] != NULL) return 0xff; |
| 247 | return 0; |
| 248 | case 0x28: |
| 249 | if(imageDiskList[3] != NULL) return 0xff; |
| 250 | return 0; |
| 251 | case 0x29: |
| 252 | if(imageDiskList[3] != NULL) return (0xc0 | (((imageDiskList[3]->heads) > 8) << 3)); |
| 253 | return 0; |
| 254 | case 0x2a: |
| 255 | if(imageDiskList[3] != NULL) return (imageDiskList[3]->cylinders & 0xff); |
| 256 | return 0; |
| 257 | case 0x2b: |
| 258 | if(imageDiskList[3] != NULL) return ((imageDiskList[3]->cylinders & 0xff00)>>8); |
| 259 | return 0; |
| 260 | case 0x2c: |
| 261 | if(imageDiskList[3] != NULL) return (imageDiskList[3]->sectors); |
| 262 | return 0; |
| 263 | case 0x39: |
| 264 | return 0; |
| 265 | case 0x3a: |
| 266 | return 0; |
| 267 | |
| 268 | |
| 269 | case 0x0b: /* Status register B */ |
| 270 | case 0x0d: /* Status register D */ |
| 271 | case 0x0f: /* Shutdown status byte */ |
| 272 | case 0x14: /* Equipment */ |
| 273 | case 0x15: /* Base Memory KB Low Byte */ |
| 274 | case 0x16: /* Base Memory KB High Byte */ |
| 275 | case 0x17: /* Extended memory in KB Low Byte */ |
| 276 | case 0x18: /* Extended memory in KB High Byte */ |
| 277 | case 0x30: /* Extended memory in KB Low Byte */ |
| 278 | case 0x31: /* Extended memory in KB High Byte */ |
| 279 | // LOG(LOG_BIOS,LOG_NORMAL)("CMOS:Read from reg %X : %04X",cmos.reg,cmos.regs[cmos.reg]); |
| 280 | return cmos.regs[cmos.reg]; |
| 281 | default: |
| 282 | LOG(LOG_BIOS,LOG_NORMAL)("CMOS:Read from reg %X",cmos.reg); |
| 283 | return cmos.regs[cmos.reg]; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void CMOS_SetRegister(Bitu regNr, Bit8u val) { |
| 288 | cmos.regs[regNr] = val; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | class CMOS:public Module_base{ |
| 293 | private: |
| 294 | IO_ReadHandleObject ReadHandler[2]; |
| 295 | IO_WriteHandleObject WriteHandler[2]; |
| 296 | public: |
| 297 | CMOS(Section* configuration):Module_base(configuration){ |
| 298 | WriteHandler[0].Install(0x70,cmos_selreg,IO_MB); |
| 299 | WriteHandler[1].Install(0x71,cmos_writereg,IO_MB); |
| 300 | ReadHandler[0].Install(0x71,cmos_readreg,IO_MB); |
| 301 | cmos.timer.enabled=false; |
| 302 | cmos.timer.acknowledged=true; |
| 303 | cmos.reg=0xa; |
| 304 | cmos_writereg(0x71,0x26,1); |
| 305 | cmos.reg=0xb; |
| 306 | cmos_writereg(0x71,0x2,1); //Struct tm *loctime is of 24 hour format, |
| 307 | cmos.reg=0xd; |
| 308 | cmos_writereg(0x71,0x80,1); /* RTC power on */ |
| 309 | // Equipment is updated from bios.cpp and bios_disk.cpp |
| 310 | /* Fill in base memory size, it is 640K always */ |
| 311 | cmos.regs[0x15]=(Bit8u)0x80; |
| 312 | cmos.regs[0x16]=(Bit8u)0x02; |
| 313 | /* Fill in extended memory size */ |
| 314 | Bitu exsize=(MEM_TotalPages()*4)-1024; |
| 315 | cmos.regs[0x17]=(Bit8u)exsize; |
| 316 | cmos.regs[0x18]=(Bit8u)(exsize >> 8); |
| 317 | cmos.regs[0x30]=(Bit8u)exsize; |
| 318 | cmos.regs[0x31]=(Bit8u)(exsize >> 8); |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | static CMOS* test; |
| 323 | |
| 324 | void CMOS_Destroy(Section* sec){ |
| 325 | delete test; |
| 326 | } |
| 327 | |
| 328 | void CMOS_Init(Section* sec) { |
| 329 | test = new CMOS(sec); |
| 330 | sec->AddDestroyFunction(&CMOS_Destroy,true); |
| 331 | } |