为什么要学操作系统

什么是操作系统

为程序提供服务的算操作系统

硬件和软件的中间层

广义: 为人提供服务都算,せかい(sekai)是一个操作系统

一点数电

数电都讲点什么

  • 一个简单的公理系统(导线,时钟,逻辑门,触发器),凭这些可以计算机世界上一切东西

  • 能支撑非常复杂的数字系统设计

    1. 导线:一直有电平的高或低
    2. 触发器(filp-flop):可存储1bit的内容,先锁存住,到时钟周期的时候再把值写进去。就如verilog中的reg

与非门

  1. 先定义与非门

    1
    2
    // 先算与门再算非门
    #define NAND(X,Y) (!((X) && (Y)))
  2. 构造NOT gate AND gate OR gate

    • NOT gate
    1
    #define NOT(X)    (NAND(X,1))
    • AND gate

      1
      #define AND(X,Y)   (NOT(NAND(X,Y))) 
    • OR gate

      1
      #define OR(X,Y)   (NAND(NOT(X),NOT(Y)))

数电 + 一点代码

C语言写数字电路,后用python做可视化,用管道符连接即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <logisim.h>
#include <stdio.h>

// Wire and registers in the circuit
wire X, Y, X1, Y1, A, B, C, D, E, F, G;
reg b1 = {.in = &X1, .out = &X};
reg b0 = {.in = &Y1, .out = &Y};

int main() {
CLOCK_CYCLE {
// 1. Propagate wire values through combinatorial logic
X1 = AND(NOT(X), Y);
Y1 = NOT(OR(X, Y));
A = D = E = NOT(Y);
B = 1;
C = NOT(X);
F = Y1;
G = X;

// 2. Edge triggering: Lock values in the flip-flops
b0.value = *b0.in;
b1.value = *b1.in;
*b0.out = b0.value;
*b1.out = b1.value;

// 3. End of a cycle; display output wire values
#define PRINT(X) printf(#X " = %d; ", X)
PRINT(A);
PRINT(B);
PRINT(C);
PRINT(D);
PRINT(E);
PRINT(F);
PRINT(G);
printf("\n");
fflush(stdout);
sleep(1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import fileinput

TEMPLATE = '''
AAAAAAAAAAAA
FF BB
FF BB
FF BB
FF BB
FEGGGGGGGGBC
EE CC
EE CC
EE CC
EE CC
DDDDDDDDDDDD
'''

# These are ANSI Escape Codes
CLEAR = '\033[2J\033[1;1f' # Clear screen and move cursor to top-left
WHITE = '\033[37m░\033[0m' # A white block
BLACK = '\033[31m█\033[0m' # A black block

for line in fileinput.input():
# Execute the input line (like "A=0; B=1; ...") as Python code; the
# variables A, B, ... will be stored in ctx.
exec(line, (ctx := {}))

# Initialize the display with a clear screen and the template.
disp = CLEAR + TEMPLATE

for ch in 'ABCDEFG':
# Determine the block color.
block = {
0: WHITE,
1: BLACK,
}.get(ctx.get(ch, 0), '?')

# Replace each character in the template with its block.
disp = disp.replace(ch, block)

print(disp)

代码我小改了一下,也可以跟着下面下载课程代码

代码来源: 南京大学操作系统课

jyy是神[1]

具体实现步骤

  • 安装教学文件

    1
    wget -r -np -nH --cut-dirs=2 -R "index.html*" "https://jyywiki.cn/os-demos/introduction/logisim/" --no-check-certificate
  • cd到文件根目录,然后make编译

  • 没配置gcc环境,这步sudo install就行了

  • 用管道符连接c文件和py文件,实现可视化效果

    1
    ./logisim | python3  seg-display.py

效果展示


  1. Yanyan’s Wiki (jyywiki.cn) ↩︎