China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 20:45
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » How to directly write to the screen View 1,704 Replies 7
Original Poster Posted 2006-06-18 14:09 ·  中国 湖南 益阳 电信
新手上路
Credits 10
Posts 3
Joined 2006-06-18 14:08
20-year member
UID 57210
Status Offline
How to directly write to the screen in VGA mode 12H using assembly? I tried putting data to address A0000, but what was displayed were pixel bars instead of pixel points.
Floor 2 Posted 2006-06-18 19:36 ·  中国 湖南 长沙 电信
中级用户
★★
Credits 282
Posts 126
Joined 2006-05-17 22:29
20-year member
UID 55724
Status Offline
VGA12h uses the bit - plane form.
Each address corresponds to 4 bit planes. So the 8 - bit data address in the video memory actually represents 8 pixels.

VGA has 4 write modes and 2 read modes. The default is write mode 0 and read mode 0.

Write mode 0: The byte written to the video memory is an 8 - bit bit mask - 0 means no change, and 1 means change the pixel. As for how to change specifically, it depends on the VGA register. It is very powerful and complex in function.

[ Last edited by zyl910 on 2006 - 6 - 18 at 19:48 ]
人类存在的目的就是试图理解人类为何存在
Floor 3 Posted 2006-06-18 19:44 ·  中国 湖南 长沙 电信
中级用户
★★
Credits 282
Posts 126
Joined 2006-05-17 22:29
20-year member
UID 55724
Status Offline
VII. Common Registers of VGA Cards

1. Timing Generator

The total timing signal that controls all VGA functions and completes some memory address decoding. There are 5 registers, sharing two I/O addresses (3C4H and 3C5H), where 3C4H is the index register address for selecting the current register.

(1) Clock Mode Register (Index 01H) Configures the timing lines of the timing generator. It generally doesn't need to be modified in common applications. The 4th bit of this register determines whether EGA/VGA uses byte or word addressing. This parameter is used when achieving smooth scrolling on the screen.

(2) Color Plane Write Enable Register (02H) Controls whether the processor is allowed to write to the color planes. By selectively enabling writing to certain color planes, specific graphics can be displayed on the screen. The default value is 0FH (all planes allowed to write).

Bit definition: Bits 7-4 Reserved. Bit 3 Allows writing to plane 3 (=1, allowed). Bit 2 Allows writing to plane 2 (=1, allowed). Bit 1 Allows writing to plane 1 (=1, allowed). Bit 0 Allows writing to plane 0 (=1, allowed).

(3) Reset Register (00H)

(4) Character Generator Selection Register (03H)

(5) Memory Mode Register (04H)

2. Graphics Controller

Performs logical operations (AND, OR, XOR, etc.) on the data to be written to VRAM. It includes 9 internal registers, sharing two I/O addresses (3CEH and 3CFH), where 3CEH is the index register address. Before performing any drawing operation, it should also set the color plane write enable register of the timing generator to select the color plane (of course, you can do nothing and use the default value).

(1) Set/Reset Register (Index 00H)

Used to store the fill data written to VRAM. This register should be used in conjunction with the Allow Set/Reset Register.

Bit definition: Bits 7-4 Reserved. Bit 3 Fill data for plane 3. Bit 2 Fill data for plane 3. Bit 1 Fill data for plane 1. Bit 0 Fill data for plane 0. Default value: All bits are 0.

(2) Allow Set/Reset Register (Index 01H)

Determines which planes receive the fill data from the Set/Reset Register. Any plane for which set/reset is disabled will be filled with the normal processor (CPU) output data.

Bit definition: (Default value is 0). Bits 7-4 Reserved. Bit 3 Allows set/reset for plane 3 (=1, allowed). Bit 2 Allows set/reset for plane 2 (=1, allowed). Bit 1 Allows set/reset for plane 1 (=1, allowed). Bit 0 Allows set/reset for plane 0 (=1, allowed).

(3) Color Compare Register (Index 02H)

Used to quickly look up a specified pixel in VRAM. The Color Compare Register can compare all 4 display plane data with the reference color in a single read memory cycle and report whether the color at each pixel position matches. This register is particularly effective for boundary lookup in graphics area filling algorithms. This register must be used in conjunction with the Mode Register (default value is 0 for all display modes).

Bit definition: Bits 7-4 Reserved. Bit 3 Color compare value for plane 3. Bit 2 Color compare value for plane 2. Bit 1 Color compare value for plane 1. Bit 0 Color compare value for plane 0.

(4) Data Shift and Function Selection Register (Index 03H)

Controls two independent functions: write data shift and logical operation on write data. The data can be shifted by 0-7 bits during the write cycle. Write mode 0 (see Mode Register) must be selected to allow shifting. The Function Selection bits provide basic hardware support for read and write operations on VRAM.

Bit definition: (Default is 0). Bits 7-5 Reserved. Bits 4-3 Function Selection bits: =0 No modification of write data. =1 Write data AND latch. =2 Write data OR latch. =3 Write data XOR latch. Bits 2-0 Data shift number of bits.

Note: If both shifting and logical operation are allowed, shifting is done first and then logical operation. Each color plane has a corresponding one-byte latch, so 32-bit data can be loaded into the latches simultaneously in one VRAM read cycle.

(5) Read Plane Selection Register (Index 04H)

Determines which color plane the processor can read from (except in color compare mode), that is, color compare mode is not controlled by it.

Bit definition: (Default is 0). Bits 7-2 Reserved. Bits 1-0 Define the readable color plane (0-3).

(6) Mode Register (Index 05H)

Most of its bits should not be modified, but two bits are useful, namely the Write Mode bits and the Color Compare Mode Enable bit. The Write Mode controls how the processor data is written into VRAM.

Note: If an application wants to modify the Mode Register, the state of bits 7-4 must be protected.

Bit definition: Bit 7 Reserved (0). Bit 6 256 colors (only for VGA). Bit 5 Shift Register Mode. Bit 4 Odd/Even Mode. Bit 3 Color Compare Mode can work (=1: can work). Bit 2 Reserved. Bits 1-0 Write Mode: =0 Processor writes directly. =1 Latch content as write data. =2 Fill the nth color plane with the value of the nth bit (0-3) of the processor data. =3 Not used.

Note: Write Mode 0: Usually used and also the default state. Allows the processor data to be written directly into VRAM, and also allows set/reset, shift, mask, AND, OR, XOR and other functions (other write modes do not allow these).

Write Mode 1: Can be used to quickly copy data from one position in VRAM to another. A single write operation by the processor to VRAM will read one byte of data from each of the 4 color planes, latch these 32 bits into the processor's latch, and a single write operation by the processor to VRAM will write 4 bytes of data back to another address in the display memory simultaneously (as long as all 4 color planes are allowed to write). This mode can be used to construct screen block fast copy or move functions.

Write Mode 2: When used in conjunction with the Bit Mask Register, it will convert compressed pixels into plane pixels and write them into the color planes.

(7) Masks Register (Index 06H)

After initialization by the BIOS mode selection operation, it should not be modified.

(8) Color Ignore Register (Index 07H)

Used in conjunction with the Color Compare Mode. This register masks specific planes from being tested during the color compare cycle.

Bit definition: Bits 7-4 Reserved. Bit i Ignore plane i (i=0,1,2,3).

(9) Bit Mask Register (Index 08H)

Used to mask certain bits so that they are not modified during read and write cycles. A bit in the Bit Mask Register being 0 means that during a display processing write, this bit will take the data from the latch, not from the processor's output data, playing a masking function. A read operation must be performed before writing to load the correct data into the latch.
人类存在的目的就是试图理解人类为何存在
Floor 4 Posted 2006-06-18 19:44 ·  中国 湖南 长沙 电信
中级用户
★★
Credits 282
Posts 126
Joined 2006-05-17 22:29
20-year member
UID 55724
Status Offline
### Talk about Direct Operations on VGA Video Memory in Assembly Programs

Recently, I've been bored and found an 80x86 assembly tutorial to study. When directly operating on video memory, I found that the book was very unclear. Before making it clear, it started with examples. (It feels like our awkward English teacher who would start talking about subjunctive mood and then jump to infinitives) So, below I'll present a simple example of use. I hope it can inspire everyone.

First, take a look at the graphics controller. Its port is 3CEH. As long as you send parameters to this port, you can set one of the 9 registers of the graphics controller as the currently operated register. Then send parameters to the register you just selected through port 3CFH.

Second, the operation I'm talking about is for VGA mode 12H, which is 640×480 16 colors.

Finally, the assembler I used is NASM. You can find it on www.sf.net.

##################################################

The indices of the registers are as follows

Register Name Index
Set/Reset Register 0
Enable Set/Reset Register 1
Color Compare Register 2
Data Rotate/Function Register 3
Read Map Select Register 4
Mode Select Register 5
Miscellaneous 6
Color Don't Care Register 7
Bit Mask Register 8





The specific functions of the registers won't be introduced here. Everyone can refer to "IBM-PC Assembly Language Programming" second edition, section 10.3. Regarding the read mode, what's said in "IBM-PC Assembly Language Programming" is relatively clear. I'll briefly introduce the write mode here.

By sending data to port 3CFH, you can operate on the selected register.

###################################################

Types of Write Modes

There are 4 types of write modes. Mode 0 is the default mode. Mode 2 is a simplified version of mode 0. Mode 3 is unique to VGA. It feels similar to mode 0. Mode 1 is for direct operation on bit planes.

###################################################

Operation Process

0 Set the screen to mode 12H
1 Set the registers through ports
2 Perform write operations on memory with segment address 0A000H.

###################################################

Simple Example
;---------------------------------------------------
;File: test.asm
;Introduction: A simple example of writing to VGA.
;Phenomenon: Vertical stripes in green and black appear on the screen.
;Language: Assembly language, using NASM assembler
;Author: Beidou Xingjun
;Email: huangxiangkui@163.com
;---------------------------------------------------

; Set VGA mode through BIOS
mov ah, 00h
mov al, 12h
int 10h


; The following sets up the VGA controller

; Set the VGA controller to write mode 2
mov dx, 3ceh
mov al, 5
out dx, al ; Select the Mode Select Register as the currently available register

mov dx, 3cfh
mov al, 2
out dx, al ; Set mode 2 as the current write mode

; Set the Bit Mask Register to make the pixels with bit 1 set among the eight pixels sent to 0A000H valid.
mov dx, 3ceh
mov al, 8
out dx, al ; Select the Bit Mask Register as the currently available register

mov dx, 3cfh
mov al, 11110000b ; Make the first four of the eight pixels valid and the last four invalid
out dx, al

; Operate on address 0A000H
; Set initial parameters
mov ax, 0A000h ; Segment address
mov es, ax
mov bx, 00000h ; Starting offset
mov al, 0Ah ; Green color
mov cx, 0FFFFh ; Loop count

; Loop and write to memory
jmp re
re:
mov , al
inc bx
loop re


The above is a simple writing program. The comments here should be quite clear. If there are any questions, I'm very welcome to discuss with everyone.

The above code has been compiled successfully with NASM. Since Win32 can't operate BIOS interrupts, I added some code to make it run under Bochs. Everything is normal.

If there are any questions, you can ask.



STUDIO Software Development Group (SDT)
STUDIO Development Team
Beidou Xingjun (Huang Xiangkui)






Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=250431
人类存在的目的就是试图理解人类为何存在
Floor 5 Posted 2006-06-19 00:02 ·  中国 福建 三明 电信
高级用户
★★
苏醒的沉睡者
Credits 659
Posts 217
Joined 2003-02-15 00:00
23-year member
UID 930
Gender Male
From 福建
Status Offline
Learning assembly can refer to "80x86 Assembly Language Programming" published by Tsinghua University Press, which is quite detailed. However, the only shortcoming is that it does not contain content about assembly programming in protected mode
好久没碰Dos,手都生了,赶紧回来练练.嘿嘿
Floor 6 Posted 2006-06-19 13:02 ·  中国 湖南 益阳 电信
新手上路
Credits 10
Posts 3
Joined 2006-06-18 14:08
20-year member
UID 57210
Status Offline
It seems quite complicated. Then I'll still use BIOS interrupts~~ But that speed is really slow. The whole screen takes several seconds to refresh once! Thanks to zyl910. Learning protected mode isn't very useful, and I'm not interested in the original kernel code~~ Hehe, it's not that I don't understand it. : )
Floor 7 Posted 2006-06-19 13:04 ·  中国 湖南 益阳 电信
新手上路
Credits 10
Posts 3
Joined 2006-06-18 14:08
20-year member
UID 57210
Status Offline
By the way, when I directly write to the screen in 13H mode, it works correctly! I didn't set the port either. It's just that the resolution in 13H is too small~~~ not fun
Floor 8 Posted 2006-06-21 19:03 ·  中国 湖南 长沙 电信
中级用户
★★
Credits 282
Posts 126
Joined 2006-05-17 22:29
20-year member
UID 55724
Status Offline
I. Special VGA Modes
After entering VGA 13h, high resolution can be achieved by modifying VGA registers.
Refer to "Graphics Programmer's Guide":
Chapter 31 Higher 256 - Color Resolutions for VGA
Chapter 32 360×480 256 - Color Mode
Chapter 49 256 - Color Animation in Mode X

Graphics Programmer's Guide (Michael Abrash's Graphics Programming Black Book):
http://www.netyi.net/Resource/933a25cf-fa0e-4dab-b9d7-e58a0ea8f52e.aspx

The advantage of this method is good compatibility.
The disadvantage is that the operation is relatively complicated.

II. VESA SVGA Standard
Using the VESA SVGA standard can easily achieve high resolutions (640*480, 800*600, 1024*768...) and high colors (15bit, 16bit, 24bit, 32bit).

VGSA Official Website:
http://www.vesa.org/

VBE (VESA BIOS Extension):
http://www.vesa.org/Public/VBE/

The advantage of this method is convenient operation and full - supported resolutions.
The disadvantage is poor compatibility, and DOS virtual machines of Windows 2000/XP/2003 do not support it.

[ Last edited by zyl910 on 2006 - 6 - 21 at 19:06 ]
人类存在的目的就是试图理解人类为何存在
Forum Jump: