读写CMOS内存 <br><br>一、CMOS内存信息详解 <br><br>一般的PC/AT、286、386、486等微机均配有CMOS芯片,CMOS&127;芯片包含了一个实时钟和64个字节的CMOS内存。在CMOS内存中,0-0DH为实时钟的有关信息,0E-&127;3FH包含计算机的硬件配置信息,如常规内存的大小、扩展内存的大小、&127;软盘的类型、固定盘的类型及其物理参数、显示器的类型等,这些参数与计算机能否正常工作具有密切的关系,另外还有计算机的开机口令和其它辅助设置信息。表1列出了&127;CMOS内存各字节的用途。 <br>表1 CMOS内存配置信息的含义 <br><br>地 址 功能 说明 <br>0,1 秒,秒报警 <br>2,3 分,分报警 <br>4,5 时,时报警 <br>6 星期几 <br>7,8,9 日,月,年 <br>A 状态寄存器A <br>B 状态寄存器B <br>C 状态寄存器C <br>D 状态寄存器D 0=电池失效,80=电池有效 <br>E 诊断状态 <br>F 关机状态 由上电诊断定义 <br>10 软驱 高4位为A驱,低4位为B驱,0=无, 1=360KB, 2=1.2KB, 4=1.44KB, 6=720KB <br>11 保留 <br>12 固定盘 高4位为C驱,低4位为D驱,0=无,F=用户定义盘, 其它为系统定义盘 <br>13 保留 <br>14 设备状态 标志驱动器数、显示器类型、有无数学处理器等 <br>15-16 内存 以KB计的常规内存数,100H=256KB,200H=512KB, 280H=640KB <br>17-18 扩展内存 以KB计的扩展内存数,200H=512KB,400H=1024KB等 <br>19 C盘类型数 根据具体硬盘类型而定 <br>1A D盘类型数 根据具体硬盘类型而定 <br>1B-1C 保留 <br>1D-1E C盘柱体数 1D-2CH只有当硬盘为用户自定义类型时起作用 <br>1F C盘面数 <br>20-21 C盘WP <br>22-23 C盘LZ <br>24 C盘每柱扇区 <br>25-26 D盘柱体数 <br>27 D盘面数 <br>28-29 D盘WP <br>2A-2B D盘LZ <br>2C D盘每柱扇区 <br>2D 保留 <br>2E-2F 校验和 为10-2DH共30个字节的和数,2EH为高位,2FH为低位 <br>30-31 扩展内存 <br>32 日期的世纪数 BCD码的世纪值,如1995年的世纪数为19 <br>33 标志信息 <br>34-3F 保留 <br><br>*地址栏均为16进制表示 <br><br>二、读写CMOS内存的方法 <br><br>CMOS内存的地址口和数据口的口地址分别为70H和71H。在对CMOS内存进行写操作时,首先将要写入的CMOS内存的地址送到口地址70H,&127;再将要写入的数据送口地址71H。在对CMOS内存进行读操作时,首先将要读出的CMOS内存的地址送到口地址70H,再从口地址71H读出数据到AL寄存器。 <br><br>三、程序设计与使用 <br><br>为了方便系统信息丢失后CMOS信息的恢复,作者用BORLAND PASCAL&127;设计了一个CMOS.PAS的程序,它可以将CMOS内存中的信息直接写入文件,也可以把文件中的信息写入CMOS内存,同时可以对CMOS内存中的信息进行编辑修改,并重新写回CMOS内存。它不仅解决了没有SETUP程序的计算机在加电时不能设置CMOS内存的问题,同时解决了CMOS信息的保存和恢复问题,是广大计算机用户的一个好帮手。 <br>该程序的使用很简单,在DOS提示符下打CMOS,即显示该程序的使用方法,&127;具体使用方法是: <br><br>CMOS [/开关] <br><br>开关有3个: <br>R --- 读取CMOS内存信息,并将其存入CMOS.DAT的文件,共占64个字节。 <br>W --- 从CMOS.DAT中读取信息,并将其写入CMOS内存。&127;注意这样写入的CMOS信息,其时间和日期是不正确的,写完之后应当用DOS命令DATE和TIME&127;设置正确的日期和时间。 <br>M --- 从CMOS中读取当前信息,进行修改,然后将其写入CMOS内存和CMOS.DAT的文件。 <br><br>四、程序清单 <br><br>由于篇幅的限制,程序中略去了用TURBO &127;VISION&127;编写的程序界面部分。 <br><br>program CMOS; <br>type <br>TCMOSType = record <br> Seconds : byte; <br> SecondAlarm : byte; <br> Minutes : byte; <br> MinuteAlarm : byte; <br> Hours : byte; <br> HourAlarm : byte; <br> DayOfWeek : byte; <br> DayOfMonth : byte; <br> Month : byte; <br> Year : byte; <br> StatusRegA : byte; <br> StatusRegB : byte; <br> StatusRegC : byte; <br> StatusRegD : byte; <br> DiagStatus : Byte; <br> ShutDownStatus : Byte; <br> FloppyDrive : byte; <br> Reserved1 : byte; <br> FixedDrive : Byte; <br> Reserved2 : byte; <br> Equipment : byte; <br> RAM : word; <br> XMS : word; <br> FixedDriveType1 : byte; <br> FixedDriveType2 : byte; <br> Reserved3 : word; <br> Cylinder1 : word; <br> Head1 : byte; <br> WP1 : word; <br> LZ1 : word; <br> Sector1 : byte; <br> Cylinder2 : word; <br> Head2 : byte; <br> WP2 : word; <br> LZ2 : word; <br> Sector2 : byte; <br> Sys : byte; <br> CheckSum : word; <br> XMS1 : word; <br> DateCentury : byte; <br> InfoFlags : byte; <br> Reserved4: array[1..12] of byte; <br>end; <br>TByte64 = array[1..64] of byte; <br>TCMOS = object <br> CMOSRec : TCMOSType; <br> procedure ReadCMOS; <br> procedure WriteCMOS; <br> procedure DisplayCMOS; <br> procedure ModifyCMOS; <br> procedure ReadFile; <br> procedure WriteFile; <br>end; <br>procedure TCMOS.ReadFile; <br>var <br> f1 : file; <br> data : tbyte64 absolute CMOSRec; <br> ch : char; <br>begin <br> write('Please input the drive name (A/B/C/D): '); <br> readln(ch); <br> assign(f1,ch+':/CMOS.DAT'); <br> reset(f1,1); <br> blockread(f1,data,sizeof(data)); <br> close(f1); <br>end; <br>procedure TCMOS.WriteFile; <br>var <br> f1:file; <br> data : tbyte64 absolute CMOSRec; <br> ch : char; <br>begin <br> write('Please input the drive name (A/B/C/D): '); <br> readln(ch); <br> assign(f1,ch+':/CMOS.DAT'); <br> rewrite(f1,1); <br> blockwrite(f1,data,sizeof(data)); <br> close(f1); <br>end; <br>procedure TCMOS.ReadCMOS; <br>begin <br>asm <br>les di,self <br>add di,CMOSRec <br>MOV CX,40H <br>MOV AH,0H <br>MOV BX,0 <br>@1: <br>MOV DX,70H <br>MOV AL,AH <br>OUT DX,AL <br>INC DX <br>in AL,dx <br>MOV BYTE PTR es:[di+BX],al <br>INC AH <br>INC BX <br>DEC CX <br>JNZ @1 <br>end; <br>end; <br>procedure TCMOS.WriteCMOS; <br>begin <br>asm <br>les di,self <br>add di,CMOSRec <br>MOV CX,40H <br>MOV AH,0H <br>MOV BX,0 <br>@1: <br>MOV DX,70H <br>MOV AL,AH <br>OUT DX,AL <br>MOV AL,BYTE PTR es:[di+BX] <br>INC DX <br>OUT DX,AL <br>INC AH <br>INC BX <br>DEC CX <br>JNZ @1 <br>end; <br>end; <br>procedure TCMOS.DisplayCMOS; <br>var <br>hd1,hd2,fd1,fd2 : byte; <br>begin <br>Writeln(^J^M'CMOS RAM information:'); <br>writeln('Date(MM-DD-YY): ',CMOSRec.Month shr 4,CMOSRec.Month and $f, <br>'-',CMOSRec.DayOfMonth shr 4,CMOSRec.DayOfMonth and $f, <br>'-',CMOSRec.Year shr 4,CMOSRec.Year and $f); <br>writeln('Time(HH:MM:SS): ',CMOSRec.Hours shr 4,CMOSRec.Hours and $f, <br>':',CMOSRec.Minutes shr 4,CMOSRec.Minutes and $f, <br>':',CMOSRec.Seconds shr 4,CMOSRec.Seconds and $f); <br>writeln('Conventional Memory: ',CMOSRec.Ram,'KB'); <br>writeln('Extended Memory: ',CMOSRec.XMS,'KB'); <br>hd2 := CMOSRec.FixedDrive and $f; <br>hd1 := CMOSRec.FixedDrive shr 4; <br>if (hd1 <> 0) then <br>begin <br>writeln('Fixed Drive 1: ',CMOSRec.FixedDriveType1); <br>writeln(' Cylinder : ',CMOSRec.Cylinder1); <br>writeln(' Head : ',CMOSRec.Head1); <br>writeln(' Sector: ',CMOSRec.Sector1); <br>writeln(' LZ: ',CMOSRec.LZ1); <br>writeln(' WP: ',CMOSRec.WP1); <br>end; <br>if (hd2 <> 0) then <br>begin <br>writeln('Fixed Drive 2: ',CMOSRec.FixedDriveType2); <br>writeln(' Cylinder : ',CMOSRec.Cylinder2); <br>writeln(' Head : ',CMOSRec.Head2); <br>writeln(' Sector: ',CMOSRec.Sector2); <br>writeln(' LZ: ',CMOSRec.LZ2); <br>writeln(' WP: ',CMOSRec.WP2); <br>end; <br>fd2 := CMOSRec.FloppyDrive and $f; <br>fd1 := CMOSRec.FloppyDrive shr 4; <br>if (fd1 <> 0) then <br>begin <br>write('Floppy Drive 1 : '); <br>case fd1 of <br>1 : writeln('360KB 5.25'''); <br>2 : writeln('1.2MB 5.25'''); <br>4 : writeln('1.44MB 3.5'''); <br>6 : writeln('720KB 3.5'''); <br>end; <br>end ; <br>if (fd2 <> 0) then <br>begin <br>write('Floppy Drive 2 : '); <br>case fd2 of <br>1 : writeln('360KB 5.25'''); <br>2 : writeln('1.2MB 5.25'''); <br>4 : writeln('1.44MB 3.5'''); <br>6 : writeln('720KB 3.5'''); <br>end; <br>end; <br>end; <br>procedure TCMOS.ModifyCMOS; <br>var <br>hd1,hd2,fd1,fd2 : byte; <br>data : tbyte64 absolute CMOSRec; <br>i : word; <br>begin <br>Writeln('Please input CORRECT CMOS information !'); <br>write('Conventional Memory (',CMOSRec.ram,'KB): ');readln(CMOSRec.ram); <br>write('Extended Memory (',CMOSRec.XMS,'KB): ');readln(CMOSRec.XMS); <br>write('Type of Fixed Disk 1: (',CMOSRec.FixedDriveType1,'): ');readln(CMOSRe <br>c.FixedDriveType1); <br>write(' Cylinder (',CMOSRec.Cylinder1,'):'); readln(CMOSRec.Cylinder1); <br>write(' Head (',CMOSRec.Head1,'): ');readln(CMOSRec.Head1); <br>write(' Sector (',CMOSRec.Sector1,'): ');readln(CMOSRec.Sector1); <br>write(' LZ (',CMOSRec.LZ1,'): ');readln(CMOSRec.LZ1); <br>write(' WP (',CMOSRec.WP1,'): ');readln(CMOSRec.WP1); <br>write('Type of Fixed Disk 2: (',CMOSRec.FixedDriveType2,'): ');readln(CMOSRe <br>c.FixedDriveType2); <br>write(' Cylinder (',CMOSRec.Cylinder2,'):'); readln(CMOSRec.Cylinder2); <br>write(' Head (',CMOSRec.Head2,'): ');readln(CMOSRec.Head2); <br>write(' Sector (',CMOSRec.Sector2,'): ');readln(CMOSRec.Sector2); <br>write(' LZ (',CMOSRec.LZ2,'): ');readln(CMOSRec.LZ2); <br>write(' WP (',CMOSRec.WP2,'): ');readln(CMOSRec.WP2); <br>hd1 := 0; hd2 :=0; <br>if (CMOSRec.FixedDriveType1>46) then hd1 := $f; <br>if (CMOSRec.FixedDriveType2>46) then hd2 := $f; <br>CMOSRec.FixedDrive := hd1 shl 4 + hd2; <br>fd2 := CMOSRec.FloppyDrive and $f; <br>fd1 := CMOSRec.FloppyDrive shr 4; <br>write('Floppy Drive 1 ('); <br>case fd1 of <br>1 : write('360KB 5.25''): '); <br>2 : write('1.2MB 5.25''): '); <br>4 : write('1.44MB 3.5''): '); <br>6 : write('720KB 3.5''): '); <br>end; <br>readln(fd1); <br>write('Floppy Drive 2 ('); <br>case fd2 of <br>1 : write('360KB 5.25''): '); <br>2 : write('1.2MB 5.25''): '); <br>4 : write('1.44MB 3.5''): '); <br>6 : write('720KB 3.5''): '); <br>end; <br>readln(fd2); <br>CMOSRec.FloppyDrive := fd1 shl 4 + fd2; <br>CMOSRec.CheckSum := 0; <br>for i := 17 to 46 do inc(CMOSRec.CheckSum,data); <br>i := CMOSRec.CheckSum; <br>data[47] := hi(i); <br>data[48] := lo(i); <br>end; <br>procedure help; <br>begin <br>WriteLn('Syntex:'+^J^M+ <br>' CMOS /R --- read information from CMOS RAM '+^J^M+ <br>' and write it to CMOS.DAT file '+^J^M+ <br>' CMOS /W --- read configuration information from CMOS.DAT '+^J^M+ <br>' and write it to CMOS RAM'); <br>Writeln(' CMOS /M --- modify CMOS information and save it'^J^M+ <br>' Floppy Drive Type:'+^J^M+ <br>' 1 : 360KB 5.25'''+^J^M+ <br>' 2 : 1.2MB 5.25'''+^J^M+ <br>' 4 : 1.44MB 3.5'''+^J^M+ <br>' 6 : 720KB 3.5'''); <br>end; <br>var ch : char; <br>temp : string; <br>ICMOS : TCMOS; <br>begin <br>WriteLn('CMOS Proctector 1.00, Copyright (c) 1995 Dong Zhanshan'); <br>if paramcount = 1 then <br>begin <br>temp := paramstr(1); <br>ch := upcase(temp[2]); <br>case ch of <br>'M' : begin <br>ICMOS.ReadCMOS; <br>ICMOS.ModifyCMOS; <br>ICMOS.DisplayCMOS; <br>ICMOS.WriteFile; <br>ICMOS.WriteCMOS; <br>end; <br>'R' : begin <br>ICMOS.ReadCMOS; <br>ICMOS.DisplayCMOS; <br>ICMOS.WriteFile; <br><br>end; <br>'W' : begin <br>ICMOS.ReadFile; <br>ICMOS.DisplayCMOS; <br>ICMOS.WriteCMOS; <br>end; <br>else help; <br>end; <br>end <br>else <br>help; <br>end. <br><br> <br>