mov dx,0x40 mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x1 x1: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x2 x2: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x3 x3: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x4 x4: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x5 x5: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x6 x6: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x7 x7: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x8 x8: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x9 x9: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x10 x10: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x11 x11: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x12 x12: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x13 x13: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x14 x14: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x15 x15: mov al,0x04 out 0x43,al in al,dx mov ah,al in al,dx xchg al,ah cmp ax,ax je x16 x16: ret mov ax,0xb800 mov es,ax lodsw and [es:di],ah or [es:di],al inc di mov ax,es mov ds,ax push ax pop ax and [si],ah or [si],al inc si and byte[si],0x12 or byte[si],0x34 inc si and word[si],0x1234 or word[si],0x5678 mov ax,cs mov ds,ax lodsw and ax,[es:di] xchg ax,bx lodsw or ax,bx stosw mov ax,[es:di] and ax,1234h or ax,5678h stosw ret mov word[8*4],int8_isav0 pushf push cs mov ax,after_ints push ax mov al,0x34 out 0x43,al mov al,76 out 0x40,al mov al,0 out 0x40,al sti hlt int8_isav0: ; Starts 792 479 push ax push dx mov dx,0x3d4 ; ; mov al,11 ; mov dx,0x3d9 ; out dx,al ; ; pop dx mov al,0xfe out 0x21,al push ds xor ax,ax mov ds,ax mov word[0x20],int8_isav1 ; pop ds mov al,0x20 out 0x20,al ; pop ax sti hlt ; ends 1042 - 250 cycles ok 733 254 cycles int8_isav1: ; starts 1401 783 ; push ax ; push dx ; mov dx,0x3d4 mov ax,0x2102 ; Horizontal sync position early out dx,ax ; mov al,12 ; mov dl,0xd9 ; out dx,al ; pop dx ; push ds ; xor ax,ax ; mov ds,ax mov word[0x20],int8_isav3 ; pop ds mov al,0x20 out 0x20,al ; pop ax ; add sp,6 sti hlt ; ends 1710 - 309 cycles 965 182 cycles int8_isav2: ; starts 783 1087 ; push ax ; push dx ; mov dx,0x3d4 mov ax,0x5a02 ; Horizontal sync position normal out dx,ax ; mov al,13 ; mov dl,0xd9 ; out dx,al ; pop dx ; push ds ; xor ax,ax ; mov ds,ax mov word[0x20],int8_isav3 ; pop ds mov al,0x20 out 0x20,al ; pop ax ; add sp,6 sti hlt ; ends 1088 - 305 cycles 1268 181 cycles ; Final 3 - scanline 227 int8_isav3: ; starts 1391 1391 ; push ax ; push dx ; mov al,14 ; mov dl,0xd9 ; out dx,al ; pop dx ; push ds ; xor ax,ax ; mov ds,ax mov word[0x20],int8_isav4 ; pop ds mov al,0x20 out 0x20,al ; pop ax ; add sp,6 sti hlt ; ends 1662 - 271 cycles ok 1549 158 cycles ; Final 4 - scanline 228 int8_isav4: ; starts 783 1695 ; push ax ; push dx ; mov dx,0x3d4 mov ax,0x2102 ; Horizontal sync position early out dx,ax ; mov al,15 ; mov dl,0xd9 ; out dx,al ; pop dx ; mov ax,[cs:delayPCycles] ; add [cs:delayTotal],ax ; mov word[cs:delayPCycles],0 ; mov ax,0 ;-1 ; add ax,520*76 ; out 0x40,al ; mov al,ah ; out 0x40,al mov al,(520*76) & 0xff out 0x40,al mov al,(520*76) >> 8 out 0x40,al ; push ds ; xor ax,ax ; mov ds,ax mov word[0x20],int8_isav5 ; pop ds mov al,0x20 out 0x20,al ; pop ax ; add sp,6 sti hlt ; ends 1249 - 466 cycles 1930 235 cycles ; Final 5 - scanline 229 int8_isav5: ; starts 1391 1999 1704 add sp,4*6 ; 6 bytes for each of isav1-isav5. isav0 will be undone with iret ; push ax ; push dx ; mov dx,0x3d4 mov ax,0x5a02 ; Horizontal sync position normal out dx,ax ; mov al,10 ; mov dl,0xd9 ; out dx,al ; pop dx mov al,(76*2) & 0xff out 0x40,al mov al,(76*2) >> 8 out 0x40,al ; push ds ; xor ax,ax ; mov ds,ax mov word[0x20],int8_isav0 pop ds pop dx mov al,0xfe ;[cs:originalIMR] out 0x21,al ; add word[cs:timerCount],76*525 ; jnc doneInterrupt8 ; pop ax ; jmp far [cs:originalInterrupt8] doneInterrupt8: mov al,0x20 out 0x20,al pop ax iret ; ends 1816 - 425 cycles ok delayPCycles: dw 0 delayTotal: dw 0 after_ints: ret mov dx,0x3d9 in al,dx test al,8 jnz foo1 foo1: in al,dx test al,8 jz foo2 foo2: mov dl,0xd9 mov al,0x0f out dx,al ret mov ax,0xb800 mov es,ax xor di,di mov cx,79 lodsb stosb add di,cx stosb add di,cx lodsb stosb add di,cx stosb add di,cx movsb add di,cx lodsb stosb add di,cx stosb add di,cx movsb add di,cx lodsb stosb add di,cx stosb add di,cx movsb add di,cx lodsb stosb add di,cx stosb add di,cx lodsb stosb add di,cx stosb add di,cx movsb add di,cx lodsb stosb add di,cx stosb add di,cx movsb add di,cx lodsb stosb add di,cx stosb add di,cx movsb add di,cx lodsb stosb add di,cx stosb add di,cx movsb add di,cx ret mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x mov di,[si] ; x*x mov bp,[bx] ; y*y lea ax,[di+bp] ; x*x+y*y cmp ax,ax jne $+2 dec cx mov bx,[si+bx] ; (x+y)*(x+y) sub bx,ax ; 2*x*y add bx,dx ; 2*x*y+b -> new y mov si,es add si,di sub si,bp ; x*x-y*y+a -> new x ret mov al,0xff mov bl,0xff imul bl jo foo foo: push ax pop ax ; mov cx,1000 ;self: ; loop self ret ; Enable auto-EOI mov al,0x13 ; ICW4 needed, not cascaded, call address interval 8, edge triggered out 0x20,al ; Set ICW1 mov al,0x08 ; Interrupt vector address out 0x21,al ; Set ICW2 mov al,0x0f ; 8086/8088 mode, auto-EOI, buffered mode/master, not special fully nested mode out 0x21,al ; Set ICW4 mov al,0xbc ; Enable IRQs 0 (timer), 1 (keyboard) and 6 (floppy disk). out 0x21,al ; Leave disabled 2 (EGA/VGA/slave 8259) 3 (COM2/COM4), 4 (COM1/COM3), 5 (hard drive, LPT2) and 7 (LPT1) xor ax,ax mov ds,ax mov word[8*4],testIRQ0 ; mov al,0x54 ; out 0x43,al ; mov al,86-63 ; out 0x41,al ; mov al,70 ; out 0x41,al mov al,0x14 out 0x43,al mov al,70-63 out 0x40,al mov al,70 out 0x40,al sti ; mov ax,0x0000 ; mul ah ; wait hlt mov ax,0xffff ;0x0000 mul ah ; wait hlt mov ax,0x0101 mul ah ; wait hlt mov ax,0x0303 mul ah ; wait hlt mov ax,0x0707 mul ah ; wait hlt mov ax,0x0f0f mul ah ; wait hlt mov al,0x34 out 0x43,al mov al,0 out 0x40,al out 0x40,al mov word[8*4],irq0 sti ret testIRQ0: ; push ax ; push bx ; mov al,0x20 ; out 0x20,al ; mov bx,sp ; inc word[ss:bx+4] ; mov ax,[ss:bx+4] ; pop bx ; pop ax ; iret ; mov bx,sp ; inc byte[ss:bx] iret mov cx,100 xor ax,ax mov ds,ax mov bp,ax mov si,ax mov di,ax mov dx,ax v: times 15 nop mov word[es:0x1234],0x5678 nop mixPatch: add bp,9999 ; 0 mov bx,bp ; 4 mov bl,99 ; 6 mov al,[bx] ; 8 add si,9999 ; 10 mov bx,si ; 14 mov bl,99 ; 16 add al,[bx] ; 18 add di,9999 ; 20 mov bx,di ; 24 mov bl,99 ; 26 add al,[bx] ; 28 add dx,9999 ; 30 mov bx,dx ; 34 mov bl,99 ; 36 add al,[bx] out 0x42,al ; Output total to speaker loopPatch: loop v mov cx,100 xor ax,ax mov ds,ax mov bp,ax mov si,ax mov di,ax mov dx,ax mov ax,0xb800 mov es,ax v: mov word[es:0x1234],0x5678 nop mixPatch: add bp,9999 ; 0 mov bx,bp ; 4 mov bl,99 ; 6 mov al,[bx] ; 8 add si,9999 ; 10 mov bx,si ; 14 mov bl,99 ; 16 add al,[bx] ; 18 add di,9999 ; 20 mov bx,di ; 24 mov bl,99 ; 26 add al,[bx] ; 28 add dx,9999 ; 30 mov bx,dx ; 34 mov bl,99 ; 36 add al,[bx] out 0x42,al ; Output total to speaker loopPatch: loop v testRoutine: mov [cs:savedSP],sp times 8 nop mov si,0x8000 mov word[si],testFarCall mov [si+2],cs mov al,0xff mov bl,0xff mul bl call far[si] testFarCall: %rep 20 cbw %endrep mov sp,[cs:savedSP] mov al,0xff mov bl,0xff mul bl db 0xf0 %rep 20 cbw %endrep mov al,0xff mov bl,0xff mul bl db 0xf1 %rep 20 cbw %endrep ret retTest: mov al,0xff mov bl,0xff mul bl ret retTest2: mov al,0xff mov bl,0xff mul bl ret 2 retfTest: mov al,0xff mov bl,0xff mul bl retf retfTest2: mov al,0xff mov bl,0xff mul bl retf 2 callTest: %rep 20 cbw %endrep ret savedSP: dw 0 mov cl,1 mov al,7 times 5 nop mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl mul cl ret mov al,0xb4 out 0x43,al mov al,7 out 0x42,al mov al,0 out 0x42,al in al,0x61 or al,3 out 0x61,al mov cx,10 rep lodsw mov dx,0x3d9 mov bl,0 mov cl,0 mul bl shl al,cl in al,dx mul bl nop shl al,cl in al,dx mul bl cbw shl al,cl in al,dx mul bl aaa shl al,cl in al,dx mul bl shl al,cl nop in al,dx mul bl shl al,cl cbw in al,dx mul bl shl al,cl aaa in al,dx inc cx mul bl shl al,cl in al,dx mul bl nop shl al,cl in al,dx mul bl cbw shl al,cl in al,dx mul bl aaa shl al,cl in al,dx mul bl shl al,cl nop in al,dx mul bl shl al,cl cbw in al,dx mul bl shl al,cl aaa in al,dx inc cx mul bl shl al,cl in al,dx mul bl nop shl al,cl in al,dx mul bl cbw shl al,cl in al,dx mul bl aaa shl al,cl in al,dx mul bl shl al,cl nop in al,dx mul bl shl al,cl cbw in al,dx mul bl shl al,cl aaa in al,dx inc cx mul bl shl al,cl in al,dx mul bl nop shl al,cl in al,dx mul bl cbw shl al,cl in al,dx mul bl aaa shl al,cl in al,dx mul bl shl al,cl nop in al,dx mul bl shl al,cl cbw in al,dx mul bl shl al,cl aaa in al,dx mov dx,0x3d9 mov bl,0 mov cl,0 mul bl cbw shl al,cl xlatb mul bl cbw nop shl al,cl xlatb mul bl cbw cbw shl al,cl xlatb mul bl cbw aaa shl al,cl xlatb mul bl cbw shl al,cl nop xlatb mul bl cbw shl al,cl cbw xlatb mul bl cbw shl al,cl aaa xlatb inc cx mul bl cbw shl al,cl xlatb mul bl cbw nop shl al,cl xlatb mul bl cbw cbw shl al,cl xlatb mul bl cbw aaa shl al,cl xlatb mul bl cbw shl al,cl nop xlatb mul bl cbw shl al,cl cbw xlatb mul bl cbw shl al,cl aaa xlatb inc cx mul bl cbw shl al,cl xlatb mul bl cbw nop shl al,cl xlatb mul bl cbw cbw shl al,cl xlatb mul bl cbw aaa shl al,cl xlatb mul bl cbw shl al,cl nop xlatb mul bl cbw shl al,cl cbw xlatb mul bl cbw shl al,cl aaa xlatb inc cx mul bl cbw shl al,cl xlatb mul bl cbw nop shl al,cl xlatb mul bl cbw cbw shl al,cl xlatb mul bl cbw aaa shl al,cl xlatb mul bl cbw shl al,cl nop xlatb mul bl cbw shl al,cl cbw xlatb mul bl cbw shl al,cl aaa xlatb mov word[cs:patch+1],jumpdest mov [cs:patch+3],cs jmp $+2 patch: db 0xea,0x00,0x00,0x00,0x00 jumpdest: mov ax,0x40 mov ds,ax ; lockstep mov dx,0x03d8 mov al,0 out dx,al ; Set up CRTC for 1 character by 2 scanline "frame". This gives us 2 lchars ; per frame. mov dx,0x3d4 ; 0xff Horizontal Total mov ax,0x0100 out dx,ax ; 0xff Horizontal Displayed 28 mov ax,0x0101 out dx,ax ; 0xff Horizontal Sync Position 2d mov ax,0x2d02 out dx,ax ; 0x0f Horizontal Sync Width 0a mov ax,0x0a03 out dx,ax ; 0x7f Vertical Total 7f mov ax,0x0104 out dx,ax ; 0x1f Vertical Total Adjust 06 mov ax,0x0005 out dx,ax ; 0x7f Vertical Displayed 64 mov ax,0x0106 out dx,ax ; 0x7f Vertical Sync Position 70 mov ax,0x0007 out dx,ax ; 0x03 Interlace Mode 02 mov ax,0x0208 out dx,ax ; 0x1f Max Scan Line Address 01 mov ax,0x0009 out dx,ax ; 256 lchars (horizonta) + 256 lchars (vertical) = 2731 CPU cycles = 114 iterations of "rep lodsw" cli cld xor ax,ax mov ds,ax mov si,ax ; Delay for enough time to refresh 512 columns mov cx,16 ; Increase refresh frequency to ensure all DRAM is refreshed before turning ; off refresh. mov al,TIMER1 | LSB | MODE2 | BINARY out 0x43,al mov al,2 out 0x41,al ; Timer 1 rate ; Each iteration takes 24 cycles and refreshes 2 locations. rep lodsw mov al,TIMER1 | LSB | MODE0 | BINARY out 0x43,al mov al,0x01 ; Count = 0x0001 so we'll stop almost immediately out 0x41,al ; Set "lodsb" destination to be CGA memory mov ax,0xb800 mov es,ax mov ds,ax mov di,0x3ffc mov si,di mov ax,0x0303 ; Found by trial and error stosw mov al,0x00 stosb mov dl,0xda ; Set argument for MUL mov cl,1 ; Go into CGA/CPU lockstep. jmp $+2 mov al,0 ; exact value doesn't matter here - it's just to ensure the prefetch queue is filled mul cl lodsb mul cl nop lodsb mul cl nop lodsb mul cl ; To get the CRTC into lockstep with the CGA and CPU, we need to figure out ; which of the four possible CRTC states we're in and switch states (by ; waiting for 2*N+1 lchars) until we're in a single state. We do this by ; waiting for the display enable bit to go low in a loop that takes 144 ; cycles per iteration. This will loop at most 3 times. mov ax,1 test al,1 jnz .loopTop1 ; Always jump to clear the prefetch queue. .loopTop1: mov al,1 div cl times 6 nop in al,dx test al,1 jnz .loopTop1 ret mov [cs:savedSP],sp xor ax,ax mov ds,ax mov word[0x20],irq0a writePIT16 0, 2, 2 mov dx,0x3d9 in al,dx test al,1 jnz .loop1 .loop1: in al,dx test al,1 jnz .loop2 .loop2: in al,dx test al,1 jz .loop3 .loop3: writePIT16 0, 2, 31 sti hlt irq0a: mov al,75 out 0x40,al mov al,0 out 0x40,al mov word[0x20],irq0b mov al,0x20 out 0x20,al sti hlt irq0b: in al,dx mov al,0x20 out 0x20,al writePIT16 0, 2, 0 mov word[0x20],irq0 mov sp,[cs:savedSP] ret mov ax,0x7000 mov es,ax mov ds,ax mov cx,5 es rep movsw ; mov cx,2246 ; mov cx,4492 ; mov cx,8984 ; mov cx,17968 ; mov cx,35936 in al,0x60 in al,0x61 in al,0x62 in al,0x63 in al,0x61 or al,1 out 0x61,al mov al,0x94 out 0x43,al mov al,2 ;3 out 0x42,al in al,0x61 mov ah,al in al,0x61 mov bl,al in al,0x61 mov bh,al in al,0x61 mov cl,al in al,0x61 mov ch,al in al,0x61 mov dl,al in al,0x61 mov dh,al in al,0x61 mov cx,1000 .l1: loop .l1 ret in al,0x09 in al,0x0a in al,0x0b in al,0x0c in al,0x0e in al,0x0f %if 0 writePIT16 0, 2, 410 mov al,3 out 0x80,al mov al,4 out 0x81,al mov al,5 ;0xb ;5 out 0x82,al mov al,6 out 0x83,al mov ax,0x6000 mov es,ax mov di,0x8000 mov ax,0x1234 stosw mov ax,0x5678 stosw ; mov ax,0xb800 ; mov es,ax ; mov di,0xc0 ; xor ax,ax ; stosw ; stosw sti ; mov al,TIMER1 | BOTH | MODE0 | BINARY ; out 0x43,al ; mov al,0 ; out 0x41,al ; mov al,1;8 ; out 0x41,al ; mov al,TIMER1 | LSB | MODE2 | BINARY ; out 0x43,al ; mov al,18 ;0x8 ; out 0x41,al out 0x0c,al ; clear byte pointer flip/flop mov al,0x3f out 0x01,al ; mov al,0 out 0x01,al ; Set channel 0 count to 64 mov al,0x00 out 0x00,al mov al,0x80 out 0x00,al ; Set channel 0 address to 0x8000 mov al,0x00 out 0x02,al mov al,0x80 out 0x02,al ; Set channel 1 address to 0x8000 mov al,0x80 out 0x04,al mov al,0 out 0x04,al ; Set channel 2 address to 0x80 mov al,0xc0 out 0x06,al mov al,0x80 out 0x06,al ; Set channel 3 address to 0x80c0 mov al,0x3f out 0x03,al ; mov al,0 out 0x03,al ; Set channel 1 count to 64 mov al,0x1f out 0x05,al ; mov al,0 out 0x05,al ; Set channel 2 count to 32 mov al,0x1f out 0x07,al ; mov al,0 out 0x07,al ; Set channel 3 count to 32 mov al,0x0f ; Set mask for all channels out 0x0f,al ; 1 = memory-to-memory, 2 = channel 0 address hold, 4 = disable, 8 = compressed timing, 0x10 = rotating priority, 0x20 = extended write, 0x40 = DREQ sense active low, 0x80 = DACK sense active high ; 1-3 = channel, 4 = write, 8 = read, 0x10 = auto-init, 0x20 = decrement, 0x00 = demand, 0x40 = single, 0x80 = block, 0xc0 = cascade ; mov al,0x10 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, normal timing, rotating priority, late write selection, DREQ sense active high, DACK sense active low ; mov al,0x01 ; Memory-to-memory enable, Channel 0 address hold disable, controller enable, normal timing, fixed priority, late write selection, DREQ sense active high, DACK sense active low ; mov al,0x03 ; Memory-to-memory enable, Channel 0 address hold enable, controller enable, normal timing, fixed priority, late write selection, DREQ sense active high, DACK sense active low ; mov al,0x09 ; Memory-to-memory enable, Channel 0 address hold disable, controller enable, compressed timing, fixed priority, late write selection, DREQ sense active high, DACK sense active low ; mov al,0x29 ; Memory-to-memory enable, Channel 0 address hold disable, controller enable, compressed timing, fixed priority, extended write selection, DREQ sense active high, DACK sense active low ; mov al,0x21 ; Memory-to-memory enable, Channel 0 address hold disable, controller enable, normal timing, fixed priority, extended write selection, DREQ sense active high, DACK sense active low ; mov al,0x29 ; Memory-to-memory enable, Channel 0 address hold disable, controller enable, compressed timing, fixed priority, extended write selection, DREQ sense active high, DACK sense active low ; mov al,0x00 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, normal timing, fixed priority, late write selection, DREQ sense active high, DACK sense active low ; mov al,0x08 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, compressed timing, fixed priority, late write selection, DREQ sense active high, DACK sense active low ; mov al,0x28 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, compressed timing, fixed priority, extended write selection, DREQ sense active high, DACK sense active low ; mov al,0x40 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, normal timing, fixed priority, late write selection, DREQ sense active low, DACK sense active low ; mov al,0x20 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, normal timing, fixed priority, extended write selection, DREQ sense active high, DACK sense active low mov al,0x10 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, normal timing, rotating priority, normal write selection, DREQ sense active low, DACK sense active low out 0x08,al ; DMA command write mov al,0x08 ; channel 0, read, no autoinit, increment, demand out 0x0b,al ; DMA mode write mov al,0x05 ; channel 1, write, no autoinit, increment, demand out 0x0b,al ; DMA mode write mov al,0x4a ; channel 2, read, no autoinit, increment, single out 0x0b,al ; DMA mode write mov al,0x47 ; channel 3, write, no autoinit, increment, single out 0x0b,al ; mov al,4 ; out 0x09,al ; request channel 0 ; mov al,5 ; out 0x09,al ; request channel 1 mov al,6 out 0x09,al ; request channel 2 mov al,7 out 0x09,al ; request channel 3 mov al,0x03 ; Set mask for channels 0 and 1, clear mask for channels 2 and 3 out 0x0f,al mov al,0xff ; mov al,0x71 ; Memory-to-memory enable, Channel 0 address hold disable, controller enable, normal timing, rotating priority, extended write selection, DREQ sense active high, DACK sense active low ; out 0x08,al ; DMA command write ; The following code will crash the machine unless refresh worked ; mov cx,65535 ;.loop: ; xchg ax,cx ; mov cx,1;8 ;.loop2: ; loop .loop2 ; xchg ax,cx ; loop .loop hlt ; db 0xff,0xff ; pop ax ; %rep 7 ; %rep 20 ; nop ; out dx,al ; Pixel is 15 cycles == 45 hdots, ; %endrep ;; mov al,8 ;; out 0x41,al ; %endrep mov ax,[es:0x8000] mov ax,[es:0x8002] mov cx,1000 .loop: loop .loop mov al,0x0f ; Set all masks out 0x0f,al mov al,0x00 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, normal timing, fixed priority, late write selection, DREQ sense active high, DACK sense active low out 0x08,al ; DMA command write mov al,0x58 ; channel 0, read, autoinit, increment, single out 0x0b,al ; DMA mode write ; mov al,TIMER1 | BOTH | MODE2 | BINARY ; out 0x43,al ; mov al,18 ; out 0x41,al ; mov al,0 ; out 0x41,al out 0x0c,al ; clear byte pointer flip/flop mov al,0xff out 0x01,al ; out 0x01,al ; Set count to 65536 mov al,0 out 0x80,al out 0x81,al out 0x82,al out 0x83,al mov al,0x0e ; Set mask for channel 1, 2 and 3, clear mask for channel 0 out 0x0f,al %endif ; 304 cycles, 15 + 50 - 4 = 61 cycles for refresh %if 0 mov al,0x0f ; Set mask for all channels out 0x0f,al mov dx,0x3d9 mov al,TIMER1 | LSB | MODE2 | BINARY out 0x43,al mov al,76 out 0x41,al out 0x0c,al ; clear byte pointer flip/flop mov al,3 out 0x01,al ; mov al,0 out 0x01,al ; Set count to 4 mov al,0x08 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, compressed timing, fixed priority, late write selection, DREQ sense active high, DACK sense active low out 0x08,al ; DMA command write mov al,0x88 ; channel 0, read, no autoinit, no increment, block out 0x0b,al ; DMA mode write mov al,0x0e ; Set mask for channels 1-3, clear mask for channel 0 out 0x0f,al %assign i 0 %rep 7 %rep 17 nop out dx,al %endrep ; mov al,i ; out 0x00,al ; mov al,(i >> 8) ; out 0x00,al ; mov al,4 ; out 0x09,al mov al,3 out 0x01,al %assign i i+4 nop nop nop %rep 17 nop out dx,al %endrep ; mov al,i ; out 0x00,al ; mov al,(i >> 8) ; out 0x00,al ; mov al,4 ; out 0x09,al mov al,0 out 0x01,al %assign i i+4 nop nop nop %endrep mov al,0x0f ; Set mask for all channels out 0x0f,al mov al,0xff out 0x01,al ; out 0x01,al ; Set count to 4 mov al,0x00 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, normal timing, fixed priority, late write selection, DREQ sense active high, DACK sense active low out 0x08,al ; DMA command write mov al,0x58 ; channel 0, read, autoinit, increment, single mode out 0x0b,al ; DMA mode write mov al,0x0e ; Set mask for channels 1-3, clear mask for channel 0 out 0x0f,al %endif ; ~312 cycles, 72 for refresh %if 0 mov dx,0x3d9 mov al,TIMER1 | LSB | MODE2 | BINARY out 0x43,al mov al,3 out 0x41,al ; mov al,64 ; out 0x41,al mov al,1 out 0x0a,al %rep 7 %rep 16 nop out dx,al %endrep ; mov al,3 ; out 0x41,al ; times 3 nop ; mov al,64 ; out 0x41,al mov al,0 out 0x0a,al nop mov al,4 out 0x0a,al %endrep mov al,0 out 0x0a,al %endif %if 1 mov al,TIMER1 | LSB | MODE0 | BINARY out 0x43,al mov al,72 out 0x41,al mov al,0x40 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, normal timing, fixed priority, late write selection, DREQ sense active low, DACK sense active low out 0x08,al ; DMA command write mov al,0x58 ; channel 0, read, autoinit, increment, single ;demand mode out 0x0b,al ; DMA mode write %rep 7 %rep 20 nop out dx,al ; Pixel is 15 cycles == 45 hdots, %endrep mov al,8 out 0x41,al %endrep mov al,0x00 ; Memory-to-memory disable, Channel 0 address hold disable, controller enable, normal timing, fixed priority, late write selection, DREQ sense active high, DACK sense active low out 0x08,al ; DMA command write mov al,0x58 ; channel 0, read, autoinit, increment, single mode out 0x0b,al ; DMA mode write %endif ret rep repne jmp $+2 ; db 0,0 ret ; mov ax,98 ; mov bl,204 ; mov cl,16 ; shr cl,cl ; div bl ; jmp $+2 ; ; mov ax,99 ; mov bl,204 ; mov cl,16 ; shr cl,cl ; div bl ; jmp $+2 ; ; mov ax,9453 ; mov bl,155 ; mov cl,16 ; shr cl,cl ; div bl ; jmp $+2 ; ; mov ax,9454 ; mov bl,155 ; mov cl,16 ; shr cl,cl ; div bl ; jmp $+2 ; ; mov ax,5726 ; mov bl,153 ; mov cl,16 ; shr cl,cl ; div bl ; jmp $+2 ; ; mov ax,5727 ; mov bl,153 ; mov cl,16 ; shr cl,cl ; div bl ; jmp $+2 mov ax,9468 mov bl,155 mov cl,16 shr cl,cl div bl jmp $+2 mov ax,9469 mov bl,155 mov cl,16 shr cl,cl div bl jmp $+2 mov ax,9455 mov bl,155 mov cl,16 shr cl,cl div bl jmp $+2 mov ax,9456 mov bl,155 mov cl,16 shr cl,cl div bl jmp $+2 ret xor bx,bx mov ds,bx mov word[bx],div0 mov [bx+2],cs mov ax,0 mov dl,0 mov cl,16 shr cl,cl idiv dl ;div0: jmp $+2 ; mov word[bx],div0a mov ax,0 mov dl,1 mov cl,16 shr cl,cl idiv dl ;div0a: jmp $+2 ; mov word[bx],div0b mov ax,128 mov dl,1 mov cl,16 shr cl,cl idiv dl ;div0b: jmp $+2 ; add sp,12 ret div0: iret mov al,0x80 out 0x43,al in al,0x42 mov bl,al in al,0x42 mov bh,al mov al,0x80 out 0x43,al in al,0x42 mov cl,al in al,0x42 mov ch,al mov al,0x80 out 0x43,al in al,0x42 mov bl,al in al,0x42 mov dh,al mov al,0x80 out 0x43,al in al,0x42 mov ah,al in al,0x42 xchg ah,al ret mov [cs:savedSP],sp mov ax,0xaa mov cl,0; 0xff db 0xd3, 0xf0 push ax pop ax ret ; mov al,TIMER1 | LSB | MODE2 | BINARY ; out 0x43,al ; mov al,REFRESH ; out 0x41,al ; Timer 1 rate ; ; xor ax,ax ; mov ds,ax ; mov word[0x20],irq0setup ; writePIT16 0, 2, 2 ; Ensure we have a pending IRQ0 ; writePIT16 0, 2, 40 ; sti ; hlt ; Should never be hit ;irq0setup: ; mov al,0x20 ; out 0x20,al ; mov word[0x20],irq0test ; sti ; hlt ; ;irq0test: mov ax,0xb800 mov ds,ax mov ax,cs mov ss,ax mov sp,1234 mov dx,0x3d4 mov bx,0x5001 mov di,0x1900 mov ax,0x5702 mov si,12345 mov bp,123 mov es,ax ; Scanlines 0-199 %macro scanline 1 mov al,0x00 out dx,ax ; e Horizontal Total left 0x5700 88 mov ax,0x0202 out dx,ax ; f Horizontal Sync Position right 0x0202 2 pop cx mov al,0x0c mov ah,ch out dx,ax inc ax mov ah,cl out dx,ax lodsb ; out 0xe0,al %if %1 == -1 mov ax,0x0104 out dx,ax ; Vertical Total times 3 nop %elif %1 == 198 mov ax,0x3f04 out dx,ax ; Vertical Total 0x3f04 64 (1 for scanlines -1 and 198, 62 for scanlines 199-260) times 3 nop %else mov al,[bp+si] mov dl,0xd9 out dx,al mov dl,0xd4 %endif mov ax,0x0101 out dx,ax ; b Horizontal Displayed right 0x0101 1 xchg ax,di out dx,ax ; a Horizontal Total right 0x1900 26 xchg ax,di xchg ax,bp out dx,ax ; d Horizontal Displayed left 0x5001 80 xchg ax,bp mov ax,es out dx,ax ; c Horizontal Sync Position left 0x5702 88 %endmacro %assign i 0 ;%rep 200 ; scanline i ; %assign i i+1 ;%endrep scanline -1 scanline 0 scanline 1 scanline 2 scanline 3 scanline 198 ; Scanline 199 mov ax,0x7100 out dx,ax ; e Horizontal Total left 0x7100 114 mov ax,0x5a02 out dx,ax ; f Horizontal Sync Position right 0x5a02 90 lodsb out 0xe0,al mov sp,[cs:savedSP] ; xor ax,ax ; mov ds,ax ; mov word[0x20],irq0 ; writePIT16 0, 2, 0 ; ; mov al,0x20 ; out 0x20,al ; ret savedSP: aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa ret mov dx,0x3d9 %rep 10 in al,dx test al,8 jz $+2 %endrep %rep 10 in al,dx test al,8 jnz $+2 %endrep mov di,data mov ax,cs mov es,ax mov dx,0x3da in al,dx stosb in al,dx stosb in al,dx stosb in al,dx stosb in al,dx stosb in al,dx stosb in al,dx stosb in al,dx stosb in al,dx stosb in al,dx stosb ; readPIT16 0 ; stosw ; readPIT16 0 ; stosw ; readPIT16 0 ; stosw ; readPIT16 0 ; stosw ; readPIT16 0 ; stosw ; ret data: refreshOn 19 mov dx,0x3d9 mov ax,0xb800 mov es,ax mov [cs:savedStack],sp mov [cs:savedStack+2],ss mov ax,0x7000 mov ss,ax mov sp,0 mov ax,0x7000 mov ds,ax ; 2 0 mov sp,[bx] ; 2 2 pop di ; 1 2 mov al,[es:di] ; 3 1 +WS pop cx ; 1 2 and ax,cx ; 2 0 pop cx ; 1 2 or ax,cx ; 2 0 stosw ; 1 2 +WS +WS pop ax ; 1 2 and ah,[es:di+1] ; 4 1 +WS pop cx or ax,cx ; 2 0 stosw ; 1 2 +WS +WS pop ax out dx,al mov ds,bp lodsb out 0xe0,al mov ax,0x7000 mov ds,ax ; 2 0 mov sp,[bx] ; 2 2 pop di ; 1 2 mov al,[es:di] ; 3 1 +WS pop cx ; 1 2 and ax,cx ; 2 0 pop cx ; 1 2 or ax,cx ; 2 0 stosw ; 1 2 +WS +WS pop ax ; 1 2 and ah,[es:di+1] ; 4 1 +WS pop cx or ax,cx ; 2 0 stosw ; 1 2 +WS +WS pop ax out dx,al mov ds,bp lodsb out 0xe0,al mov ax,0x7000 mov ds,ax ; 2 0 mov sp,[bx] ; 2 2 pop di ; 1 2 mov al,[es:di] ; 3 1 +WS pop cx ; 1 2 and ax,cx ; 2 0 pop cx ; 1 2 or ax,cx ; 2 0 stosw ; 1 2 +WS +WS pop ax ; 1 2 and ah,[es:di+1] ; 4 1 +WS pop cx or ax,cx ; 2 0 stosw ; 1 2 +WS +WS pop ax out dx,al mov ds,bp lodsb out 0xe0,al mov sp,[cs:savedStack] mov ss,[cs:savedStack+2] ret savedStack: dw 0,0 cld cld ; mov ds,bp ; 2 0 pop ax ; 1 2 out dx,al ; 1 1 mov dx,0x3d4 mov ax,0 out dx,ax