cmos内存的读写 <br><br><br>一、CMOS内存信息详解 <br>一般的PC/AT、286、386、486等微机均配有CMOS芯片,CMOS&127;芯片包含了一个实时钟 <br>和64个字节的CMOS内存。在CMOS内存中,0-0DH为实时钟的有关信息,0E-&127;3FH包含 <br>计算机的硬件配置信息,如常规内存的大小、扩展内存的大小、&127;软盘的类型、固定 <br>盘的类型及其物理参数、显示器的类型等,这些参数与计算机能否正常工作具有密切的 <br>关系,另外还有计算机的开机口令和其它辅助设置信息。表1列出了&127;CMOS内存各字 <br>节的用途。 <br>表1 CMOS内存配置信息的含义 <br>地 址 <br> 功能 <br> 说明 <br>0,1 <br> 秒,秒报警 <br> <br>2,3 <br> 分,分报警 <br> <br>4,5 <br> 时,时报警 <br> <br>6 <br> 星期几 <br> <br>7,8,9 <br> 日,月,年 <br> <br>A <br> 状态寄存器A <br> <br>B <br> 状态寄存器B <br> <br>C <br> 状态寄存器C <br> <br>D <br> 状态寄存器D <br> 0=电池失效,80=电池有效 <br>E <br> 诊断状态 <br> <br>F <br> 关机状态 <br> 由上电诊断定义 <br>10 <br> 软驱 <br> 高4位为A驱,低4位为B驱,0=无, 1=360KB, 2=1.2KB, 4=1.44KB, 6=720KB <br>11 <br> 保留 <br> <br>12 <br> 固定盘 <br> 高4位为C驱,低4位为D驱,0=无,F=用户定义盘, 其它为系统定义盘 <br>13 <br> 保留 <br> <br>14 <br> 设备状态 <br> 标志驱动器数、显示器类型、有无数学处理器等 <br>15-16 <br> 内存 <br> 以KB计的常规内存数,100H=256KB,200H=512KB, 280H=640KB <br>17-18 <br> 扩展内存 <br> 以KB计的扩展内存数,200H=512KB,400H=1024KB等 <br>19 <br> C盘类型数 <br> 根据具体硬盘类型而定 <br>1A <br> D盘类型数 <br> 根据具体硬盘类型而定 <br>1B-1C <br> 保留 <br> <br>1D-1E <br> C盘柱体数 <br> 1D-2CH只有当硬盘为用户自定义类型时起作用 <br>1F <br> C盘面数 <br> <br>20-21 <br> C盘WP <br> <br>22-23 <br> C盘LZ <br> <br>24 <br> C盘每柱扇区 <br>25-26 <br> D盘柱体数 <br> <br>27 <br> D盘面数 <br> <br>28-29 <br> D盘WP <br>2A-2B <br> D盘LZ <br>2C <br> D盘每柱扇区 <br>2D <br> 保留 <br>2E-2F <br> 校验和 <br> 为10-2DH共30个字节的和数,2EH为高位,2FH为低位 <br>30-31 <br> 扩展内存 <br>32 <br> 日期的世纪数 <br> BCD码的世纪值,如1995年的世纪数为19 <br>33 <br> 标志信息 <br>34-3F <br> 保留 <br> <br> *地址栏均为16进制表示 <br>二、读写CMOS内存的方法 <br>CMOS内存的地址口和数据口的口地址分别为70H和71H。在对CMOS内存进行写操作时,首先 <br>将要写入的CMOS内存的地址送到口地址70H,&127;再将要写入的数据送口地址71H。在对 <br>CMOS内存进行读操作时,首先将要读出的CMOS内存的地址送到口地址70H,再从口地址71H <br>读出数据到AL寄存器。 <br>三、程序设计与使用 <br>为了方便系统信息丢失后CMOS信息的恢复,作者用BORLAND PASCAL&127;设计了一个CMOS <br>.PAS的程序,它可以将CMOS内存中的信息直接写入文件,也可以把文件中的信息写入CMOS <br>内存,同时可以对CMOS内存中的信息进行编辑修改,并重新写回CMOS内存。它不仅解决了 <br>没有SETUP程序的计算机在加电时不能设置CMOS内存的问题,同时解决了CMOS信息的保存 <br>和恢复问题,是广大计算机用户的一个好帮手。 <br>该程序的使用很简单,在DOS提示符下打CMOS,即显示该程序的使用方法,&127;具体使用 <br>方法是: <br>CMOS [/开关] <br>开关有3个: <br>R --- 读取CMOS内存信息,并将其存入CMOS.DAT的文件,共占64个字节。 <br>W --- 从CMOS.DAT中读取信息,并将其写入CMOS内存。&127;注意这样写入的CMOS信息,其 <br>时间和日期是不正确的,写完之后应当用DOS命令DATE和TIME&127;设置正确的日期和时间 <br>。 <br>M --- 从CMOS中读取当前信息,进行修改,然后将其写入CMOS内存和CMOS.DAT的文件。 <br>四、程序清单 <br>由于篇幅的限制,程序中略去了用TURBO &127;VISION&127;编写的程序界面部分。 <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>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.