0


myos3 大学生利用C++构建一个完整的操作系统之代码重构并实现键盘打字和鼠标移动

myos1 大学生利用C++构建一个完整的操作系统打印helloworld
myos2 大学生利用C++构建一个完整的操作系统之响应键盘中断
myos3 大学生利用C++构建一个完整的操作系统之代码重构并实现键盘打字和鼠标移动

1. 按键中断

计算机自制操作系统(十六):中断—键盘驱动 - 知乎 (zhihu.com)

在中断

IDT

256

个中断源产生的中断在IDT表中注册时, 全部指向了同一个中断服务程序

class InterruptHandler

, 其实是不准确的, 因为并没有建立各自中断号和中断服务程序之间的一一对应关系, 所以接下来利用按键中断进行演示, 展示通过按键类继承中断服务器程序来实现按键的实际中断操作;

// keyboard.h
class KeyBoardDriver: public InterruptHandler { // 这里面继承中断服务程序, 在键盘初始化时
public:
    KeyBoardDriver(InterruptManager *manager);
    ~KeyBoardDriver();
    virtual uint32_t HandleInterrupt(uint32_t esp); // 继承与中断管理器中的中断处理方法
private:
    Port8Bit dataport; // 数据端口
    Port8Bit commandport;  // 命令端口
};

1.1 键盘中断的初始化

#mermaid-svg-ibqo4uZnQTWCSMCZ .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .label text{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .node rect,#mermaid-svg-ibqo4uZnQTWCSMCZ .node circle,#mermaid-svg-ibqo4uZnQTWCSMCZ .node ellipse,#mermaid-svg-ibqo4uZnQTWCSMCZ .node polygon,#mermaid-svg-ibqo4uZnQTWCSMCZ .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ibqo4uZnQTWCSMCZ .node .label{text-align:center;fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .node.clickable{cursor:pointer}#mermaid-svg-ibqo4uZnQTWCSMCZ .arrowheadPath{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-ibqo4uZnQTWCSMCZ .flowchart-link{stroke:#333;fill:none}#mermaid-svg-ibqo4uZnQTWCSMCZ .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-ibqo4uZnQTWCSMCZ .edgeLabel rect{opacity:0.9}#mermaid-svg-ibqo4uZnQTWCSMCZ .edgeLabel span{color:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-ibqo4uZnQTWCSMCZ .cluster text{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-ibqo4uZnQTWCSMCZ .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ibqo4uZnQTWCSMCZ text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-ibqo4uZnQTWCSMCZ .actor-line{stroke:grey}#mermaid-svg-ibqo4uZnQTWCSMCZ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .sequenceNumber{fill:#fff}#mermaid-svg-ibqo4uZnQTWCSMCZ #sequencenumber{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ #crosshead path{fill:#333;stroke:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .messageText{fill:#333;stroke:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-ibqo4uZnQTWCSMCZ .labelText,#mermaid-svg-ibqo4uZnQTWCSMCZ .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-ibqo4uZnQTWCSMCZ .loopText,#mermaid-svg-ibqo4uZnQTWCSMCZ .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-ibqo4uZnQTWCSMCZ .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-ibqo4uZnQTWCSMCZ .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ibqo4uZnQTWCSMCZ .noteText,#mermaid-svg-ibqo4uZnQTWCSMCZ .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-ibqo4uZnQTWCSMCZ .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-ibqo4uZnQTWCSMCZ .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-ibqo4uZnQTWCSMCZ .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-ibqo4uZnQTWCSMCZ .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ .section{stroke:none;opacity:0.2}#mermaid-svg-ibqo4uZnQTWCSMCZ .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-ibqo4uZnQTWCSMCZ .section2{fill:#fff400}#mermaid-svg-ibqo4uZnQTWCSMCZ .section1,#mermaid-svg-ibqo4uZnQTWCSMCZ .section3{fill:#fff;opacity:0.2}#mermaid-svg-ibqo4uZnQTWCSMCZ .sectionTitle0{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .sectionTitle1{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .sectionTitle2{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .sectionTitle3{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-ibqo4uZnQTWCSMCZ .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ .grid path{stroke-width:0}#mermaid-svg-ibqo4uZnQTWCSMCZ .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-ibqo4uZnQTWCSMCZ .task{stroke-width:2}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskText:not([font-size]){font-size:11px}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-ibqo4uZnQTWCSMCZ .task.clickable{cursor:pointer}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskText0,#mermaid-svg-ibqo4uZnQTWCSMCZ .taskText1,#mermaid-svg-ibqo4uZnQTWCSMCZ .taskText2,#mermaid-svg-ibqo4uZnQTWCSMCZ .taskText3{fill:#fff}#mermaid-svg-ibqo4uZnQTWCSMCZ .task0,#mermaid-svg-ibqo4uZnQTWCSMCZ .task1,#mermaid-svg-ibqo4uZnQTWCSMCZ .task2,#mermaid-svg-ibqo4uZnQTWCSMCZ .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskTextOutside0,#mermaid-svg-ibqo4uZnQTWCSMCZ .taskTextOutside2{fill:#000}#mermaid-svg-ibqo4uZnQTWCSMCZ .taskTextOutside1,#mermaid-svg-ibqo4uZnQTWCSMCZ .taskTextOutside3{fill:#000}#mermaid-svg-ibqo4uZnQTWCSMCZ .active0,#mermaid-svg-ibqo4uZnQTWCSMCZ .active1,#mermaid-svg-ibqo4uZnQTWCSMCZ .active2,#mermaid-svg-ibqo4uZnQTWCSMCZ .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-ibqo4uZnQTWCSMCZ .activeText0,#mermaid-svg-ibqo4uZnQTWCSMCZ .activeText1,#mermaid-svg-ibqo4uZnQTWCSMCZ .activeText2,#mermaid-svg-ibqo4uZnQTWCSMCZ .activeText3{fill:#000 !important}#mermaid-svg-ibqo4uZnQTWCSMCZ .done0,#mermaid-svg-ibqo4uZnQTWCSMCZ .done1,#mermaid-svg-ibqo4uZnQTWCSMCZ .done2,#mermaid-svg-ibqo4uZnQTWCSMCZ .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-ibqo4uZnQTWCSMCZ .doneText0,#mermaid-svg-ibqo4uZnQTWCSMCZ .doneText1,#mermaid-svg-ibqo4uZnQTWCSMCZ .doneText2,#mermaid-svg-ibqo4uZnQTWCSMCZ .doneText3{fill:#000 !important}#mermaid-svg-ibqo4uZnQTWCSMCZ .crit0,#mermaid-svg-ibqo4uZnQTWCSMCZ .crit1,#mermaid-svg-ibqo4uZnQTWCSMCZ .crit2,#mermaid-svg-ibqo4uZnQTWCSMCZ .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-ibqo4uZnQTWCSMCZ .activeCrit0,#mermaid-svg-ibqo4uZnQTWCSMCZ .activeCrit1,#mermaid-svg-ibqo4uZnQTWCSMCZ .activeCrit2,#mermaid-svg-ibqo4uZnQTWCSMCZ .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-ibqo4uZnQTWCSMCZ .doneCrit0,#mermaid-svg-ibqo4uZnQTWCSMCZ .doneCrit1,#mermaid-svg-ibqo4uZnQTWCSMCZ .doneCrit2,#mermaid-svg-ibqo4uZnQTWCSMCZ .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-ibqo4uZnQTWCSMCZ .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-ibqo4uZnQTWCSMCZ .milestoneText{font-style:italic}#mermaid-svg-ibqo4uZnQTWCSMCZ .doneCritText0,#mermaid-svg-ibqo4uZnQTWCSMCZ .doneCritText1,#mermaid-svg-ibqo4uZnQTWCSMCZ .doneCritText2,#mermaid-svg-ibqo4uZnQTWCSMCZ .doneCritText3{fill:#000 !important}#mermaid-svg-ibqo4uZnQTWCSMCZ .activeCritText0,#mermaid-svg-ibqo4uZnQTWCSMCZ .activeCritText1,#mermaid-svg-ibqo4uZnQTWCSMCZ .activeCritText2,#mermaid-svg-ibqo4uZnQTWCSMCZ .activeCritText3{fill:#000 !important}#mermaid-svg-ibqo4uZnQTWCSMCZ .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-ibqo4uZnQTWCSMCZ g.classGroup text .title{font-weight:bolder}#mermaid-svg-ibqo4uZnQTWCSMCZ g.clickable{cursor:pointer}#mermaid-svg-ibqo4uZnQTWCSMCZ g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ibqo4uZnQTWCSMCZ g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-ibqo4uZnQTWCSMCZ .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-ibqo4uZnQTWCSMCZ .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ibqo4uZnQTWCSMCZ .dashed-line{stroke-dasharray:3}#mermaid-svg-ibqo4uZnQTWCSMCZ #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ .commit-id,#mermaid-svg-ibqo4uZnQTWCSMCZ .commit-msg,#mermaid-svg-ibqo4uZnQTWCSMCZ .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-ibqo4uZnQTWCSMCZ g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-ibqo4uZnQTWCSMCZ g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-ibqo4uZnQTWCSMCZ g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-ibqo4uZnQTWCSMCZ .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-ibqo4uZnQTWCSMCZ .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-ibqo4uZnQTWCSMCZ .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-ibqo4uZnQTWCSMCZ .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-ibqo4uZnQTWCSMCZ .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-ibqo4uZnQTWCSMCZ .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-ibqo4uZnQTWCSMCZ .edgeLabel text{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-ibqo4uZnQTWCSMCZ .node circle.state-start{fill:black;stroke:black}#mermaid-svg-ibqo4uZnQTWCSMCZ .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-ibqo4uZnQTWCSMCZ #statediagram-barbEnd{fill:#9370db}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-state .divider{stroke:#9370db}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-ibqo4uZnQTWCSMCZ .note-edge{stroke-dasharray:5}#mermaid-svg-ibqo4uZnQTWCSMCZ .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-ibqo4uZnQTWCSMCZ .error-icon{fill:#522}#mermaid-svg-ibqo4uZnQTWCSMCZ .error-text{fill:#522;stroke:#522}#mermaid-svg-ibqo4uZnQTWCSMCZ .edge-thickness-normal{stroke-width:2px}#mermaid-svg-ibqo4uZnQTWCSMCZ .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-ibqo4uZnQTWCSMCZ .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-ibqo4uZnQTWCSMCZ .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-ibqo4uZnQTWCSMCZ .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-ibqo4uZnQTWCSMCZ .marker{fill:#333}#mermaid-svg-ibqo4uZnQTWCSMCZ .marker.cross{stroke:#333}

:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-ibqo4uZnQTWCSMCZ {
color: rgba(0, 0, 0, 0.75);
font: ;
}

      中断控制器8259
     

      CPU
     

      键盘按键(8048芯片)
     

      8042芯片
     

键盘中断的具体流程就是当我们点击键盘时, 键盘上面的芯片8048会把键盘扫描码发送给主板上的8042, 而8042是按照字节码处理数据, 并存储到

输出缓冲区

, 然后8042会给中断代理 8059A发送中断信号, 这样中断控制器通过读取8042的输出缓冲区寄存器, 获得键盘的扫描码; 总的来讲, 操作系统其实是在对8042这个芯片进行数据的操作(读写)来识别中断的

动手写操作系统9----键盘&鼠标中断实现_大魔王-CSDN博客

请添加图片描述

键盘中断通过主8259A的IRQ1触发, 而CPU通过中断向量号来寻址待执行的中断代码

    中
   
   
    断
   
   
    向
   
   
    量
   
   
    号
   
   
    =
   
   
    起
   
   
    始
   
   
    向
   
   
    量
   
   
    号
   
   
    +
   
   
    中
   
   
    断
   
   
    请
   
   
    求
   
   
    号
   
  
  
   中断向量号 = 起始向量号 + 中断请求号
  
 
中断向量号=起始向量号+中断请求号 即 

 
  
   
    H
   
   
    a
   
   
    r
   
   
    d
   
   
    w
   
   
    a
   
   
    r
   
   
    e
   
   
    I
   
   
    n
   
   
    t
   
   
    e
   
   
    r
   
   
    r
   
   
    u
   
   
    p
   
   
    t
   
   
    O
   
   
    f
   
   
    f
   
   
    s
   
   
    e
   
   
    t
   
   
    +
   
   
    0
   
   
    x
   
   
    01
   
  
  
   HardwareInterruptOffset+0x01
  
 
HardwareInterruptOffset+0x01

如此相当于在中断描述符表中注册了键盘中断对应的向量, 然后在重写中断处理函数

HandleInterrupt

就能实现按键中断

剩下的端口定义以及对端口的读写, 见i8042 键盘控制器-------详细介绍 - LinKArftc - 博客园 (cnblogs.com)

同时还需要有一个知识点 电脑键盘的通码与断码 :

类型:通码(make code)和断码(break code)。

  1. 当一个键被按下或持续按住时,键盘会将该键的通码发送给主机;
  2. 而当一个键被释放时,键盘会将该键的断码发送给主机。

根据键盘按键扫描码的不同,在此可将按键分为如下几类:

  1. 第一类按键,通码为1字节, 断码为 0 x F 0 + 通 码 形 式 0xF0+通码形式 0xF0+通码形式。如A键,其通码为 0 x 1 C 0x1C 0x1C,断码为 0 x F 0 , 0 x 1 C 0xF0, 0x1C 0xF0,0x1C
  2. 第二类按键,通码为2字节,$ 0xE0 + 0xNN$形式,断码为 0 x E 0 + 0 x F 0 + 0 x N N 0xE0+0xF0+0xNN 0xE0+0xF0+0xNN形式。如 right ctrl键,其通码为 0 x E 00 x 14 0xE0 0x14 0xE00x14,断码为 0 x E 00 x F 00 x 14 0xE0 0xF0 0x14 0xE00xF00x14,
  3. 第三类特殊按键有两个,print screen键通码为 0 x E 00 x 120 x E 00 x 7 C 0xE0 0x12 0xE0 0x7C 0xE00x120xE00x7C,断码为 0 x E 00 x F 00 x 7 C 0 x E 00 x F 00 x 12 0xE0 0xF0 0x7C 0xE0 0xF0 0x12 0xE00xF00x7C0xE00xF00x12; pause键通码为 0 x E 10 x 140 x 770 x E 10 x F 00 x 140 x F 00 x 77 0xE1 0x14 0x77 0xE1 0xF0 0x14 0xF0 0x77 0xE10x140x770xE10xF00x140xF00x77; 断码为空。

通码断码表如下:

在这里插入图片描述

根据上述知识点, 可以获得下面的对应端口读写来初始化键盘 :

// keyboard.cpp#include"drivers/keyboard.h"
KeyBoardDriver::KeyBoardDriver(InterruptManager* manager):InterruptHandler(0x01+ manager->HardwareInterruptOffset(), manager),dataport(0x60),// 这里设定键盘控制器的数据端口commandport(0x64){// 这里设定键盘控制器的命令端口while(commandport.Read()&0x01){// 清空键盘的input_buffer
        dataport.Read();}
    commandport.Write(0xae);// 激活键盘
    commandport.Write(0x20);// 8042芯片, 要读取一个字节uint8_t status =(dataport.Read()|1)&~0x10;// 开启键盘中断
    commandport.Write(0x60);// 告诉控制器, 我要开始写入了
    dataport.Write(status);
    dataport.Write(0xf4);printf("finish keyboard init ! \n");}

1.2 键盘中断的处理函数

这是一个简单的键盘中断的处理函数, 打印不同按键被按下和弹起后, 8042芯片所读到的字节码printf到屏幕上

// 这里会产生两次按键中断, 是因为啥呢, 是因为啥呢, 是因为按下和弹起uint32_t KeyBoardDriver::HandleInterrupt(uint32_t esp){printf("have keyboard Interrupt !!! \n");uint8_t key = dataport.Read();char* foo =(char*)"KEYBOARD 0X00  ";constchar* hex ="0123456789ABCDEF";
    foo[11]= hex[(key >>4)&0x0f];
    foo[12]= hex[key &0x0f];printf((constchar*)foo);printf("\n");return esp;}

1.3 结果

请添加图片描述

2. 鼠标中断

源码见我的gitee

2.1 鼠标中断初始化

其实鼠标中断和键盘中断是一样的, 都是通过对寄存器进行读写, 只不过鼠标中断的控制器在每次读写时, 还有三个字节的数据, 鼠标每一次动作都是3个字节数据,为什么是3个。想想也知道:两个坐标,一个状态;

#ifndef__MOUSE_H__#define__MOUSE_H__#include"common/types.h"#include"hardware/interrupts.h"#include"hardware/port.h"

using namespace myos;
using namespace myos::common;
using namespace myos::hardware;// 鼠标中断是IQ2
class MouseDriver: public InterruptHandler {
public:MouseDriver(InterruptManager *manager);~MouseDriver();
    virtual uint32_tHandleInterrupt(uint32_t esp);// 继承与中断管理器中的中断处理方法
private:
    Port8Bit dataport;// 数据端口
    Port8Bit commandport;// 命令端口uint8_t buffer[3];// 每次mouse读取数据是数据流, // 两种模式: 三个字节和四个字节的包, 这里是使用的是三个字节的包// xy的信息和左右中拿个按下的信息, x移动的距离, y移动的距离// 触发一次中断, 读取一个字节uint8_t offset;// 读取的哪个字节uint8_t buttons;// 哪个按键被按下int8_t x, y;// 鼠标位置初始化};

这里面的初始化设置中间点显示白色


MouseDriver::MouseDriver(InterruptManager* manager):InterruptHandler(0x0C+ manager->HardwareInterruptOffset(), manager),dataport(0x60),// 这里设定键盘鼠标控制器的数据端口commandport(0x64),// 这里设定键盘鼠标控制器的命令端口offset(0),buttons(0),x(40),y(12){// 默认鼠标在中间uint16_t* VideoMemory =(uint16_t*)0xb8000;
    VideoMemory[y *80+ x]=((VideoMemory[y *80+ x]&0xf000)>>4)|((VideoMemory[y *80+ x]&0x0f00)<<4)|(VideoMemory[y *80+ x]&0x00ff);// 让中间的点变白

    commandport.Write(0xa8);
    commandport.Write(0x20);uint8_t status =(dataport.Read()|2)&~0x20;// 按位设置开启鼠标
    commandport.Write(0x60);// 告诉我们要些数据了
    dataport.Write(status); 
    commandport.Write(0xd4);// 写入鼠标寄存器写数据了
    dataport.Write(0xf4);// 告诉键盘或者数据开启鼠标
    dataport.Read();// 读取数据流printf("finish mouse init ! \n");}

2.2 中断处理函数

鼠标的中断处理函数
根据每次read到的三个字节的数据, 进行xy位置以及不同状态的显示

uint32_t MouseDriver::HandleInterrupt(uint32_t esp){uint8_t status = commandport.Read();if(!(status &0x20))return esp;// 不是鼠标就直接返回esp

    buffer[offset]= dataport.Read();// 先读一个数据
    offset =(offset +1)%3;// offset偏移一位// offset为0, 则表示x, y操作过了, 只需要控制一下x,y即可if(offset ==0){uint16_t* VideoMemory =(uint16_t*)0xb8000;
        VideoMemory[y *80+ x]=((VideoMemory[y *80+ x]&0xf000)>>4)|((VideoMemory[y *80+ x]&0x0f00)<<4)|(VideoMemory[y *80+ x]&0x00ff);

        x += buffer[1];if(x <0) x =0;elseif(x >=80) x =79;

        y -= buffer[2];if(y <0) y =0;elseif(y >=25) y =24;

        VideoMemory[y *80+ x]=((VideoMemory[y *80+ x]&0xf000)>>4)|((VideoMemory[y *80+ x]&0x0f00)<<4)|(VideoMemory[y *80+ x]&0x00ff);for(uint8_t i =0; i <3; i++){// 判断buf是否是按键按下, 如果按下就进行翻转一下, 这里只需要判断buffer[0]的下三位表示左中右, if((buffer[0]&(1<< i))!=(buttons &(1<< i))){
                VideoMemory[y *80+ x]=((VideoMemory[y *80+ x]&0xf000)>>4)|((VideoMemory[y *80+ x]&0x0f00)<<4)|(VideoMemory[y *80+ x]&0x00ff);}}
        buttons = buffer[0];}return esp;}
标签: c++ 操作系统

本文转载自: https://blog.csdn.net/qq_32460819/article/details/121733689
版权归原作者 落子无悔! 所有, 如有侵权,请联系我们删除。

“myos3 大学生利用C++构建一个完整的操作系统之代码重构并实现键盘打字和鼠标移动”的评论:

还没有评论