- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发操作系统作为个人爱好,以便我可以学习软件工程和计算机体系结构。
当文本到达底部或 VGA_HEIGHT 时,我试图让 VGA 终端“滚动”。我将 OSDev wiki 中的代码与我自己的代码混合使用。
我的目标是复制每一行,然后将其写入其正上方的行。
这是我正在使用的代码:
void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_putchar(char c) {
unsigned char uc = c;
switch(c) {
case NEWLINE:
terminal_row++;
terminal_column = 0;
terminal_putentryat(' ', terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
break;
case '\t':
/* TODO: Implement tab */
terminal_column += 4;
break;
default:
terminal_putentryat(uc, terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
terminal_row = 0;
}
}
if(terminal_row >= VGA_HEIGHT) {
terminal_print_error();
terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--)
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
}
if(terminal_row >= VGA_HEIGHT) {
terminal_print_error();
terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--)
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
/*
* This is the screen driver. It contains functions which print
* characters and colors to the screen
* using the VGA controller.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <kernel/tty.h>
#include "vga.h"
#define REG_SCREEN_CTRL 0x3D4
#define REG_SCREEN_DATA 0x3D5
#define NEWLINE 0x0A
#define TAB 0x09
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
static uint16_t *const VGA_MEMORY = (uint16_t *)0xC03FF000;
static size_t terminal_row;
static size_t terminal_column;
static uint8_t terminal_color;
static uint16_t *terminal_buffer;
int get_offset(int col, int row) {
return 2 * (row * VGA_WIDTH + col);
}
int get_offset_row(int offset) {
return offset / (2 * VGA_WIDTH);
}
int get_offset_col(int offset) {
return (offset - (get_offset_row(offset) * 2 * VGA_WIDTH)) / 2;
}
static void scroll() {
if(terminal_row >= VGA_HEIGHT) {
}
}
void terminal_print_error(void) {
if(terminal_row >= VGA_HEIGHT) {
terminal_row = 0;
/* print white/red E to bottom right corner of screen */
terminal_putentryat('E', vga_entry_color(VGA_COLOR_RED, VGA_COLOR_WHITE),
VGA_WIDTH - 1, VGA_HEIGHT - 1);
}
}
void terminal_initialize(void) {
terminal_row = 0;
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_CYAN);
terminal_buffer = VGA_MEMORY;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
}
}
void terminal_setcolor(uint8_t color) {
terminal_color = color;
}
void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_putchar(char c) {
unsigned char uc = c;
switch(c) {
case NEWLINE:
terminal_row++;
terminal_column = 0;
terminal_putentryat(' ', terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
break;
case '\t':
/* TODO: Implement tab */
terminal_column += 4;
break;
default:
terminal_putentryat(uc, terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
terminal_row = 0;
}
}
if(terminal_row >= VGA_HEIGHT) {
terminal_print_error();
terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--)
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
}
void terminal_write(const char *data, size_t size) {
for (size_t i = 0; i < size; i++)
terminal_putchar(data[i]);
}
void terminal_writestring(const char *data) {
terminal_write(data, strlen(data));
}
/* inb */
unsigned char port_byte_in(unsigned short port) {
unsigned char result;
asm ("in %%dx, %%al" : "=a" (result) : "d" (port));
return result;
}
void port_byte_out(unsigned short port, unsigned char data) {
asm ("out %%al, %%dx" : : "a" (data), "d" (port));
}
int get_cursor_offset() {
/* Use the VGA ports to get the current cursor position
* 1. Ask for high byte of the cursor offset (data 14)
* 2. Ask for low byte (data 15)
*/
port_byte_out(REG_SCREEN_CTRL, 14);
int offset = port_byte_in(REG_SCREEN_DATA) << 8; /* High byte: << 8 */
port_byte_out(REG_SCREEN_CTRL, 15);
offset += port_byte_in(REG_SCREEN_DATA);
return offset * 2; /* Position * size of character cell */
}
void set_cursor_offset(int offset) {
/* Similar to get_cursor_offset, but instead of reading we write data */
offset /= 2;
port_byte_out(REG_SCREEN_CTRL, 14);
port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset >> 8));
port_byte_out(REG_SCREEN_CTRL, 15);
port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset & 0xff));
}
void update_cursor(int x, int y) {
uint16_t pos = y * VGA_WIDTH + x;
port_byte_out(REG_SCREEN_CTRL, 15);
port_byte_out(REG_SCREEN_DATA, (uint8_t)(pos & 0xFF));
port_byte_out(REG_SCREEN_CTRL, 14);
port_byte_out(REG_SCREEN_DATA, (uint8_t)(pos >> 8) & 0xFF);
}
/*
void enable_cursor(uint8_t cursor_start, uint8_t cursor_end) {
outb(0x3D4, 0x0A);
outb(0x3D, (inb(0x3D5) & 0xC0) | cursor_start);
outb(0x3D4, 0x0B);
outb(0x3D5, (inb(0x3D5) & 0xE0) | cursor_end);
}
*/
最佳答案
我相信循环滚动的工作大部分是正确的,但是在它导致您遇到的一些错误之前有很多代码。
当光标宽度超过屏幕右边缘时,switch 语句中的默认情况是增加光标行。 if (++terminal_row == VGA_HEIGHT)
如果行增量超过屏幕的底部边缘,则将光标行重置为 0。这可以防止滚动代码永远运行。您应该删除 if (++terminal_row == VGA_HEIGHT) terminal_row = 0;
并替换为 terminal_row++;
因为紧跟在 switch 之后的逻辑处理终端行变量。
我建议分离修改 terminal_row
的逻辑和 terminal_column
来自验证、重置和滚动这些变量的逻辑。例如,您对 '\t' 字符的处理,如果放在一行的最后 3 个字符内,会将字符溢出到下一行而不更新 terminal_row
和 terminal_column
vars 到他们应该在的地方。
terminal_putentryat
在将光标修改为换行符之后,而不是之前。事实上,你不应该这样做terminal_putentryat
对于任何换行符,因为明显没有字符改变,只有光标位置。 \t
的处理方式调用 terminal_write(' ');
而不是直接修改列变量。这简化了实际更新终端的逻辑。上面的第二段详细介绍了此更改解决的一些问题。 update_cursor()
应该只调用一次,在 terminal_putchar()
结束时因为你输入的每个字符都应该更新游标。如果您愿意,这可能会改变 terminal_putchar()
处理 0 宽度的字符,但这对我来说似乎违反直觉,因为此函数专门用于处理显示的字符。 terminal_row >= VGA_HEIGHT
从不将 terminal_row 重置为有效值。它确实调用了 terminal_print_error()
但是当您想将行保留在底部时,此函数会将您的行重置为 0。void terminal_putchar(char c) {
unsigned char uc = c;
// Handle character output and terminal_row/column modification
switch(c) {
case NEWLINE:
terminal_row++;
terminal_column = 0;
break;
case '\t':
terminal_write(' ');
break;
default:
terminal_putentryat(uc, terminal_color, terminal_column, terminal_row);
terminal_column++;
}
// Handle validation on terminal_column before terminal_row, since the logic in terminal_column can update terminal_row
if(terminal_column >= VGA_WIDTH) {
terminal_column = 0;
terminal_row++;
}
// Handle validating terminal_row, and scrolling the screen upwards if necessary.
if(terminal_row >= VGA_HEIGHT) {
// You shouldn't need terminal_print_error() since you are handling the case where terminal_row >= VGA_HEIGHT
// terminal_print_error();
// What does this line do? Appears to set the 16th character of the 16th row to the same value as the 5th character of the 1st row.
// terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--) {
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
// Also clear out the bottom row
for(i = 0; i < VGA_WIDTH-1; i++) {
terminal_putentryat(' ', terminal_color, i, VGA_HEIGHT-1);
}
terminal_row = VGA_HEIGHT-1;
}
update_cursor(terminal_column, terminal_row);
}
关于gcc - OSDev:为什么我的 VGA 终端滚动不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53128322/
我正在尝试在 Conda 环境中编译一些代码,在那里我 之前安装的编译包gcc_linux-64 . 然而,即使在停用和重新激活环境之后,gcc还在/usr/bin/gcc . 我该怎么做才能让 Co
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
这其实是两个问题: 1 - 在我的 debian amd64 系统上,我似乎无法构建与 gmp/mpfr/mpc 动态链接的交叉 GCC。即使我删除 --disable-shared,它也总是静态链接
研究ELF格式的结果,可以看到目标文件中有一个符号对应每个函数,对应的符号表项的值为st_size,表示大小的功能。 问题是,即使我更改了目标文件中特定函数的 st_size 并链接了它,但可执行文件
海湾合作委员会的 documentation for #line directives说他们是这样的: #line "myfile.cpp" 123 但是当我用 g++ 5.1 检查输出时,它们实际上
我正在使用 as 和 gcc 来汇编和创建 ARM 汇编程序的可执行文件,正如 this 所推荐的那样教程,如下: 给定一个汇编源文件,program.s,我运行: as -o program.o p
long long x; double n; x=long long(n); 这不起作用。什么是正确的方法? 最佳答案 显而易见的: x = (long long) n; 关于gcc - 转换为长长
我想知道用于 gcc 的原子内置函数的头文件是什么? 我想使用这 2 个函数为我当前创建的线程库实现互斥锁。 bool __sync_bool_compare_and_swap (type *ptr,
它出现在 another question :gcc调用的程序和部件是什么? (特别是在编译 C 或 C++ 时)以便有人可以设计一些拦截和更改流程的方案以用于各种自定义编码目的? 最佳答案 编译器二
可能吗?我想使用 gcc喜欢 assembler并在将其编译为 ubuntu 上的可执行文件后。 我尝试过这个: gcc a.asm -o out.o 来自 out.o文件编译成.out可执行文件。
我写了一个简单的 C 程序 test.c : #include #include int add(int a, int b); int main() { int i=5,j=10;
即。所以如果你使用任何八进制文字,它会给你一个警告。 微软编译器的同样问题。 如果没有,是否有任何其他工具可以检测八进制文字。 (vim 似乎有一个很酷的技巧,它突出了第一个领先的将不同的颜色归零,但
我在旧线程中搜索。但没有找到任何线程回答我的问题。 gcc 是否像 vc++ 一样支持函数级链接? 如果是,我应该提供什么选项来链接目标文件和库? 最佳答案 看起来 gcc 不直接支持函数级链接。您可
也许标题并没有把问题说得那么准确:我知道当我运行 gcc foo.c 时,GCC 会调用其他为它完成所有工作的子程序,从而生成主 gcc 程序只是一个界面。但这究竟是如何完成的呢? 它是否使用syst
我听说最近版本的 gcc 非常擅长将通过函数指针的调用转换为直接调用。但是,我在网上或快速浏览 gcc 的源代码上找不到任何关于它的信息。有谁知道这是否真的是真的,如果是这样,它使用什么算法来做到这一
gcc/g++ 链接器选项“-Map”生成的“.map”文件用于什么? 以及如何阅读它们? 最佳答案 我建议为您投入生产的任何软件生成一个映射文件并保留一份副本。 它可用于破译崩溃报告。根据系统的不同
gcc信息文件在有关x86-64特定标志的部分中说 其他事情: There is no `-march=generic' option because `-march' ind
我想知道 gcc 链接器选项(例如:-Wl,options)是否可以更改编译后的可执行文件中的汇编指令,因为如果您使用某些 gcc 优化选项会发生这种情况? 当您比较编译后的二进制文件(例如比较签名)
是否有GCC编译指示会停止,暂停或中止编译过程? 我正在使用gcc 4.1,但也希望在gcc 3.x版本上也可以使用该编译指示。 最佳答案 您可能需要#error: edd@ron:/tmp$ g++
当我使用gcc编译C程序时我通常使用 -g 将一些调试信息放入 elf 文件中这样 gdb 就可以在需要时帮助我。 但是,我注意到有些程序使用 -ggdb,因为它应该使调试信息对 gdb 更加友好。
我是一名优秀的程序员,十分优秀!