'******************************************************************************
'* 
'* GENERATION OF CMOS (NVRAM) VALUES
'*
'* Version history:
'*  - initial code by WadiM
'*
'****************************************************************************** 

dim b as byte, i as integer

//Temporary cmos cells
use object MEMORY_STREAM as cells : cells.Size=64

//RTC (real-time clock) //TODO - currently simple 0
'0..9 - updated by RTC/CMOS chip
cells.Byte(0x5)=24 'alarm hour value (which not raise alarm)

//RTC State registers //TODO
cells.Byte(0x0A)=0x26 	'state registe A
cells.Byte(0x0B)=0x70 	'state registe B (bit 2 - BCD/Binary mode)
cells.Byte(0x0C)=0 	'state registe C 
cells.Byte(0x0D)=0x80 	'state registe A (power good)

//POST
cells.Byte(0x0E)=0 	'POST h/w error byte

//Disk types
cells.Byte(0x10)=0x44 	'FDD types (1,44 both)
cells.Byte(0x12)=0x10 	'HDD types (0 and 1, bits 0-3 - HDD0, bits 4-7 - HDD1)
//cells.Byte(0x12)=0xFF 	'HDD types (0 and 1, bits 0-3 - HDD0, bits 4-7 - HDD1)
//cells.Byte(0x19)=0x47 	'HDD types (0 and 1, bits 0-3 - HDD0, bits 4-7 - HDD1)

//Computer configuration 
b=0x61 '2FDD, 80x25 CGA, no FPU (by default)
if cpu.HasFPU then b=b or 2 'FPU installed

//if pc.VideoName<>"CGA" then b=b and (not 0x30) 'EGA/VGA
cells.Byte(0x14)=b

//Conventional memory size (640Kb=280h)
cells.Word(0x15)=0x280

//Configured extended memory size in Kb (above 1 Mb)
i=shr(mem.Size,20) : if i>0 then i=i-1 'Mb count
i=shl(i,10) : if i>0xFFFF then i=0xFFFF 'Kb count
cells.Word(0x17)=i 'Kb

//CMOS control sum
cells.Word(0x2E)=0

//Detected extended memory size in Kb (above 1 Mb)
i=shr(mem.Size,20) : if i>0 then i=i-1 'Mb count
i=shl(i,10) : if i>0xFFFF then i=0xFFFF 'Kb count
cells.Word(0x30)=i 'Kb

//Century in BCD (21)
cells.Byte(0x32)=0x21

//Memory flags
cells.Byte(0x33)=0

//Checksum calculation (cells [10h..2Dh]) and writing to [2E..2F]
dim w as word : for i=0x10 to 0x2D : w=w+cells.Byte(i) : next 
cells.Byte(0x2E)=shr(w,8) : cells.Byte(0x2F)=w //TOCHECK - inverse order?

//Filling CMOS with values (via ports)
for i=0 to cells.Size-1 : pc.WritePort(0x70,i) 
pc.WritePort(0x71,cells.Byte(i)) : next

//Acknowledge interrupts
pc.WritePort(0x70,0xC) : pc.ReadPort(0x71)

