复习
本次课回答的问题
本次课主要内容
CPU 只是 “无情的指令执行机器”
如何用计算机实现核弹发射箱?
I/O 设备 (CPU 视角):“
说人话
“COM1”; putch()
的实现
#define COM1 0x3f8
static int uart_init() {
outb(COM1 + 2, 0); // 控制器相关细节
outb(COM1 + 3, 0x80);
outb(COM1 + 0, 115200 / 9600);
...
}
static void uart_tx(AM_UART_TX_T *send) {
outb(COM1, send->data);
}
static void uart_rx(AM_UART_RX_T *recv) {
recv->data = (inb(COM1 + 5) & 0x1) ? inb(COM1) : -1;
}
IBM PC/AT 8042 PS/2 (Keyboard) Controller
0x60
(data), 0x64
(status/command)Command Byte | Use | 说明 |
---|---|---|
0xED | LED 灯控 | ScrollLock/NumLock/CapsLock |
0xF3 | 设置重复速度 | 30Hz - 2Hz; Delay: 250 - 1000ms |
0xF4/0xF5 | 打开/关闭 | N/A |
0xFE | 重新发送 | N/A |
0xFF | RESET | N/A |
参考 AbstractMachine 的键盘部分实现
ATA (Advanced Technology Attachment)
0x1f0 - 0x1f7
; secondary: 0x170 - 0x177
void readsect(void *dst, int sect) {
waitdisk();
out_byte(0x1f2, 1); // sector count (1)
out_byte(0x1f3, sect); // sector
out_byte(0x1f4, sect >> 8); // cylinder (low)
out_byte(0x1f5, sect >> 16); // cylinder (high)
out_byte(0x1f6, (sect >> 24) | 0xe0); // drive
out_byte(0x1f7, 0x20); // command (write)
waitdisk();
for (int i = 0; i < SECTSIZE / 4; i ++)
((uint32_t *)dst)[i] = in_long(0x1f0); // data
}
打印机:将字节流描述的文字/图形打印到纸张上
例子:PostScript (1984)
如果你只造 “一台计算机”
但如果你希望给未来留点空间?
提供
lspci -tv
和 lsusb -tv
: 查看系统中总线上的设备pci-probe.c (AbstractMachine, x86-64/i386)
-soundhw ac97
的运行选项for (int bus = 0; bus < 256; bus++)
for (int slot = 0; slot < 32; slot++) {
uint32_t info = pciconf_read(bus, slot, 0, 0);
uint16_t id = info >> 16, vendor = info & 0xffff;
if (vendor != 0xffff) {
printf("%02d:%02d device %x by vendor %x", bus, slot, id, vendor);
if (id == 0x100e && vendor == 0x8086) {
printf(" <-- This is an Intel e1000 NIC card!");
}
printf("\n");
}
}
CPU 有一个中断引脚
系统中的其他设备可以向中断控制器连线
假设程序希望写入 1 GB 的数据到磁盘
for (int i = 0; i < 1 GB / 4; i++) {
outl(PORT, ((u32 *)buf)[i]);
}
能否把 CPU 从执行循环中解放出来?
memcpy_to_port(ATA0, buf, length);
DMA: memcpy
” 程序的 CPU
支持的几种 memcpy
PCI 总线支持 DMA
DMA 不就是一个 “做一件特别事情” 的 CPU 吗
例如,显示图形
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++)
putchar(j <= i ? '*' : ' ');
putchar('\n');
}
难办的是性能:NES: 6502 @ 1.79Mhz; IPC = 0.43
76543210
||||||||
||||||++- Palette
|||+++--- Unimplemented
||+------ Priority
|+------- Flip horizontally
+-------- Flip vertically
CPU 只
如果我们有更多的晶体管?
2D 图形加速硬件:图片的 “裁剪” + “拼贴”
实现 3D
GameBoy Advance
三维空间中的三角形需要正确渲染
例子:GLSL (Shading Language)
一个完整的众核多处理器系统
什么是 “深度神经网络”?
如何 “训练”?
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28*28, 512), nn.ReLU(),
nn.Linear(512, 512), nn.ReLU(),
nn.Linear(512, 10), nn.ReLU(), )
...
model = NeuralNetwork().to('cuda')
能完成 “同一件事” 的部件可能有很多