diff --git a/docs/books/CTP.md b/docs/books/CTP.md index 37e91f44..26582355 100644 --- a/docs/books/CTP.md +++ b/docs/books/CTP.md @@ -31,7 +31,7 @@ C Traps and Pitfalls 本章我们关注字符是如何组成符号的。 -- 术语 **token** (符号)是语言的基本表意单元。字符组成符号。例子: `->`、`file` 都是符号. +- 术语 **token** (符号)是语言的基本表意单元。字符组成符号。例子: `->`、`file` 都是符号。 - 同一组字符序列在**不同上下文**中可能属于不同符号。 我们来看几个例子,请思考这些程序的行为(注意运算符优先级)。 diff --git a/docs/courses-zju/general-physics-2.md b/docs/courses-zju/general-physics2/index.md similarity index 99% rename from docs/courses-zju/general-physics-2.md rename to docs/courses-zju/general-physics2/index.md index cb1d2f72..1260b2ef 100644 --- a/docs/courses-zju/general-physics-2.md +++ b/docs/courses-zju/general-physics2/index.md @@ -28,3 +28,6 @@ 当初学多元微积分没学好,散度旋度啥的都忘光了。现在复习一下。 + + + diff --git a/docs/courses-zju/general-physics2/math.md b/docs/courses-zju/general-physics2/math.md new file mode 100644 index 00000000..fb042e31 --- /dev/null +++ b/docs/courses-zju/general-physics2/math.md @@ -0,0 +1,52 @@ +# 数学基础 + +## 几种坐标系 + +二维平面上的: + +- 极坐标变换:$x=r\cos\theta,y=r\sin\theta$ + - 雅可比行列式为 $r$。 + +三维空间中的: + +- 球坐标变换:$x=r\sin\theta\cos\phi,y=r\sin\theta\sin\phi,z=r\cos\theta$ + - 两个角分别是与 $x$ 轴和 $z$ 轴的夹角。 + - 雅可比行列式为 $r^2\sin\theta$,与 $z$ 轴夹角。 +- 柱坐标变换:$x=r\cos\theta,y=r\sin\theta,z=z$ + - 雅可比行列式为 $r$。 + +一些概念 + +- 立体角:以观测点为球心,构造一个单位球面;任意物体投影到该单位球面上的投影面积,即为该物体相对于该观测点的立体角。$\Omega = \frac{S}{r^2}$,其中 $S$ 为球面上的面积,$r$ 为球心到球面的距离。 + - 球面坐标系中,任意球面的极小面积为:$\mathrm{d}A = (r\sin\theta\,\mathrm{d}\varphi)(r \mathrm{d}\theta)=r^2(\sin\theta\,\mathrm{d}\theta\,\mathrm{d}\varphi)$,极小立体角为 $\mathrm{d}\Omega = \frac{\mathrm{d}A}{r^2} = \sin\theta\,\mathrm{d}\theta\,\mathrm{d}\varphi$ + - 对极小立体角做曲面积分即可得立体角:$\Omega= \iint_S \mathrm{d}\Omega = \iint_S \sin\theta\,\mathrm{d}\theta\,\mathrm{d}\varphi$ + - 定向曲面:$\Omega= \iint_S \frac{dA}{r^{2}} =\iint_S \frac { \vec{r} \cdot \textrm{d}\vec{S}}{\left| \vec{r} \right|\, r^2}= \iint_S \frac { \vec{r} \cdot \textrm{d}\vec{S}}{ r^3}$。 + - 计算例:顶角为 $2\theta$ 的圆锥的立体角为 $\int_0^{2\pi} \int_0^{\theta} \sin \theta' \ d \theta' \ d \phi = 2\pi\int_0^{\theta} \sin \theta' \ d \theta' = 2\pi\left[ -\cos \theta' \right]_0^{\theta} \ = 2\pi\left(1 -\cos \theta \right)$. + + +## 其他 + +### 幂次前缀 + +| Prefix | Symbol | Factor | +| ------ | ------ | ------ | +| yotta | Y | $10^{24}$ | +| zetta | Z | $10^{21}$ | +| exa | E | $10^{18}$ | +| peta | P | $10^{15}$ | +| tera | T | $10^{12}$ | +| giga | G | $10^{9}$ | +| mega | M | $10^{6}$ | +| kilo | k | $10^{3}$ | +| hecto | h | $10^{2}$ | +| deca | da | $10^{1}$ | +| deci | d | $10^{-1}$ | +| centi | c | $10^{-2}$ | +| milli | m | $10^{-3}$ | +| micro | μ | $10^{-6}$ | +| nano | n | $10^{-9}$ | +| pico | p | $10^{-12}$ | +| femto | f | $10^{-15}$ | +| atto | a | $10^{-18}$ | +| zepto | z | $10^{-21}$ | +| yocto | y | $10^{-24}$ | \ No newline at end of file diff --git a/docs/courses-zju/general-physics2/static_electric.md b/docs/courses-zju/general-physics2/static_electric.md new file mode 100644 index 00000000..0668fd29 --- /dev/null +++ b/docs/courses-zju/general-physics2/static_electric.md @@ -0,0 +1,47 @@ +# 电学 + +## 静止电荷的电场 + +### 电荷 库仑定律 + +- 电荷是相对论不变量,与运动无关。 +- $e=1.6\times10^{-19}\mathrm{C}$ +- 库仑定律 +- $\varepsilon_0$ 真空电容率(permittivity of free space)或真空介电常数(dieletric constant of vacuum)。 +- 库仑定律只适用于点电荷。与叠加原理结合能够求解静电学中所有问题。 + +$$ +\begin{array}{l} +\vec{F}=\frac{1}{4\pi\varepsilon_0}\frac{q_1q_2}{r^2}\hat{r} \\ +k = \frac{1}{4\pi\varepsilon_0}=9\times10^9\mathrm{N\cdot m^2/C^2} \\ +\varepsilon_0=8.85\times10^{-12}\mathrm{C^2/N\cdot m^2} +\end{array} +$$ + + +### 静电场 电场强度 + +- 没有物质作传递介质的超距作用是不存在的。 +- 电磁场是物质存在的一种形态,分布在一定范围的空间里,具有能量、动量等属性,并通过交换场量子来实现相互作用的传递。电磁场的媒介子是光子,电荷间相互作用的传递速度也是电磁场的运动速度,光速。 +- 静电场是**相对于观察者静止的电荷**在其周围激发的电场。 +- 电场强度:$\vec{E}=\frac{\vec{F}}{q}$ + - 电场强度形成矢量场 $\vec{E}(\vec{r})$。 + - 电场强度叠加原理。 + - 连续分布电荷电场强度:$\vec{E}=\int \mathrm{d}\vec{E}=k\int \frac{\mathrm{d}q}{r^2}\hat{r}$ + + +!!! example "电偶极子" + + 两个大小相等符号相反的电荷,间距为 $l$,比所考虑的场点到它们的距离小得多时,称为电偶极子(electric dipole)。电偶极子的电偶极矩(electric dipole moment)为 $\vec{p}=q\vec{l}$,方向由负电荷指向正电荷。 + + - 在轴线延长线上计算电场强度:$\vec{E_A}=\frac{1}{4\pi\varepsilon_0}\frac{2\vec{p}}{x^3}$ + - 在中垂线上计算电场强度:$\vec{E_B}=-\frac{1}{4\pi\varepsilon_0}\frac{\vec{p}}{y^3}$ + + 远离电偶极子处场强与距离三次方成反比,与电偶极矩成正比。 + + + +!!! example "均匀带电直棒" + + 无限长带点直棒附近某点电场强度 $E = \frac{1}{4\pi\varepsilon_0}\frac{2\lambda}{r}$,其中 $\lambda$ 为电荷线密度。以上结论对靠近有限长直棒中部区域也近似成立。 + diff --git a/docs/courses-zjucs/digital-logic/hw1.md b/docs/courses-zjucs/digital-logic/hw1.md index a5a7909d..44d6cbc7 100644 --- a/docs/courses-zjucs/digital-logic/hw1.md +++ b/docs/courses-zjucs/digital-logic/hw1.md @@ -1,4 +1,40 @@ -# Homework 1 +
+
+
+ 校名 +
+




+
+ 校徽 +
+







+ Logic and Computer Design Fundamentals +

Homework 1

+
+
+ + + + + + + + + + + + + + + + + + + +
Teacher马德
Name 朱宝林
Student ID 3220106026
Date2023.10.06
+
+ + ## 1-3 Number Systems @@ -25,4 +61,129 @@ | Decimal | Binary | Octal | Hex | | ------- | ------ | ----- | --- | -| 369.3125 | 101110001.0101 | 561.24 | 171.4 | +| 369.3125 | 1 0111 0001.0101 | 561.24 | 171.5 | +| 189.625 | 1011 1101.101 | 275.5 | BD.A | +| 214.625 | 1101 0110.1010 | 326.5 | D6.A | +| 61831.625 | 1111 0011 1100 1110.1010 | 171716.5 | F3C7.A | + +## 1-12 Binary Multiplications + +(a) + +$$ +\begin{equation*}\begin{array}{c} +\phantom{\times9999}1010\\ +\underline{\times\phantom{9999}1100}\\ +\phantom{\times9999999}0\\ +\phantom{\times999999}0\phantom9\\ +\phantom{\times99}1010\phantom{99}\\ +\underline{\phantom{\times9}1010\phantom{999}}\\ +\phantom{\times9}1111000 +\end{array}\end{equation*} +$$ + +(b) + +$$ +\begin{equation*}\begin{array}{c} +\phantom{\times9999}0110\\ +\underline{\times\phantom{9999}1001}\\ +\phantom{\times9999}0110\\ +\phantom{\times999999}0\phantom9\\ +\phantom{\times99999}0\phantom{99}\\ +\underline{\phantom{\times9}0110\phantom{999}}\\ +\phantom{\times99}110110 +\end{array}\end{equation*} +$$ + +(c) + +$$ +\begin{equation*}\begin{array}{c} +\phantom{\times9999}1111001\\ +\underline{\times\phantom{99999}011101}\\ +\phantom{\times9999}1111001\\ +\phantom{\times999999999}0\phantom9\\ +\phantom{\times99}1111001\phantom{99}\\ +\phantom{\times9}1111001\phantom{999}\\ +\underline{\phantom{\times}1111001\phantom{9999}}\\ +\phantom{1}110110110101 +\end{array}\end{equation*} +$$ + +## 1-13 Binary Divisions + +``` + 1 0001 + --------- + 101 | 101 0110 + 101 + --- + 0110 + 0101 + ---- + 1 +``` + +Quotient: $ 1 0001 $, Remainder: $ 1 $ + +## 1-16 Radix + +(a) + +$$ +B\times r^2 + E\times r^1 + E\times r^0 = 2699\\ +\Rightarrow r = 15 +$$ + +(b) + +$$ +3\times r^2 + 6\times r^1 + 5\times r^0 = 194\\ +\Rightarrow r = 7 +$$ + +## 1-18 BCD + +(a) + +Decimal: 4867 +Binary: 0001 0011 0000 0011 + +(b) + +Decimal: 378.75 +Binary: 0001 0111 1010.1100 + +## 1-19 BCD + +$$ +(715)_{10} = (0111\ 0001\ 0101)_{BCD}\\ +(354)_{10} = (0011\ 0101\ 0100)_{BCD} +$$ + +## 1-28 Gray code + +| Binary | Gray Code | Hexdecimal | +| ------ | --------- | ---------- | +| 0000 | 0000 | 0 | +| 0001 | 0001 | 1 | +| 0010 | 0011 | 2 | +| 0011 | 0010 | 3 | +| 0100 | 0110 | 4 | +| 0101 | 0111 | 5 | +| 0110 | 0101 | 6 | +| 0111 | 0100 | 7 | +| 1000 | 1100 | 8 | +| 1001 | 1101 | 9 | +| 1010 | 1111 | A | +| 1011 | 1110 | B | +| 1100 | 1010 | C | +| 1101 | 1011 | D | +| 1110 | 1001 | E | +| 1111 | 1000 | F | + + + + + diff --git a/docs/courses-zjucs/digital-logic/hw2.md b/docs/courses-zjucs/digital-logic/hw2.md new file mode 100644 index 00000000..f29012c0 --- /dev/null +++ b/docs/courses-zjucs/digital-logic/hw2.md @@ -0,0 +1,110 @@ +
+
+
+ 校名 +
+




+
+ 校徽 +
+







+ Logic and Computer Design Fundamentals +

Homework 1

+
+
+ + + + + + + + + + + + + + + + + + + +
Teacher马德
Name 朱宝林
Student ID 3220106026
Date2023.10.06
+
+ + + +## 2-1a + +Truth Table: + +|$X$ |$Y$|$Z$| $\overline{XYZ}$ | $\overline{X}+\overline{Y}+\overline{Z}$ | +|----|---|---|-----------------|---------| +| 0 | 0 | 0 | 1 | 1 | +| 0 | 0 | 1 | 1 | 1 | +| 0 | 1 | 0 | 1 | 1 | +| 0 | 1 | 1 | 1 | 1 | +| 1 | 0 | 0 | 1 | 1 | +| 1 | 0 | 1 | 1 | 1 | +| 1 | 1 | 0 | 1 | 1 | +| 1 | 1 | 1 | 0 | 0 | + +## 2-2a, c + +$$ +\overline{X}\overline{Y} + \overline{X}Y + XY\\ += \overline{X}(\overline{Y}+Y) + XY\\ += \overline{X} + XY\\ += \overline{X} + X \text{ (Additional equation)} +$$ + +$$ +Y+\overline{X}Z+X\overline{Y}\\ += Y(\overline{X}+X) + \overline{X}Z + X\overline{Y}\\ += X + \overline{X}(Y+Z)\\ += X+ Y + Z \text{ (Additional equation)} +$$ + +## 2-3a, c + +$$ +AB\overline{C} + B\overline{C}\overline{D} + BC + \overline{C}D\\ +=B(C + A\overline{C}) + B\overline{C}\overline{D} + \overline{C}D\\ +=BC + B\overline{C}\overline{D} + \overline{C}D \text{(Absorption Law)}\\ +=B(C + \overline{C}\overline{D}) + \overline{C}D\\ +=B + \overline{C}D \text{(Absorption Law)}\\ +$$ + +$$ +A\overline{D} + \overline{A}B + \overline{C}D + \overline{B}C\\ + += (\overline{A} + \overline{B} + \overline{C} + \overline{D})(A + B + C + D) +$$ + +## 2-6b, d + + + +## 2-10a, c + +## 2-11a, c, d + +## 2-12b + +## 2-15 + +## 2-17 + +## 2-19a + +## 2-22a + +## 2-25b + +## 2-29 + +## 2-30 + +## 2-31 \ No newline at end of file diff --git a/docs/courses-zjucs/digital-logic/verilog.md b/docs/courses-zjucs/digital-logic/verilog.md index f84ca2fc..2f009552 100644 --- a/docs/courses-zjucs/digital-logic/verilog.md +++ b/docs/courses-zjucs/digital-logic/verilog.md @@ -8,17 +8,190 @@ ## 工具 +提前准备、学习以下工具,它们用于 Verilog 的编译、仿真、综合等。 +- `verilator`:Verilog 编译器 +看看系统贯通课的一个 `Makefile`: + +```makefile +DIR_SRC := src +DIR_BUILD := build + +VERILATOR_TOP := Adder_1 +VERILATOR_SRCS := $(shell find $(DIR_SRC) -name "*.v" -o -name "*.cpp") +VERILATOR_TFLAGS := -Wno-WIDTH +VERILATOR_FLAGS := --trace --cc --exe --Mdir $(DIR_BUILD) --top-module $(VERILATOR_TOP) -o $(VERILATOR_TOP) -I$(DIR_SRC) + +.PHONY: all wave clean + +all: clean + verilator $(VERILATOR_TFLAGS) $(VERILATOR_FLAGS) $(VERILATOR_SRCS) + make -C $(DIR_BUILD) -f V$(VERILATOR_TOP).mk $(VERILATOR_TOP) + cd $(DIR_BUILD); ./$(VERILATOR_TOP) + +wave: + gtkwave $(DIR_BUILD)/V$(VERILATOR_TOP).vcd + +clean: + rm -rf build +``` + +编译时抄抄就好。注意到其中几个关键的参数: + +- `--top-module`:编译出的模块名 + +`verilator` 将在 `DIR_BUILD` 编译出一大堆文件,还有一个 `.mk`,这是一个 `Makefile`。所以上面又 `make -C` 进入该目录使用该 `Makefile` 编译得到可执行文件,名称就是 `VERILATOR_TOP` 指定的。运行即可。此时 Testbench 也在里面。也可以看到 `gtkwave` 使用了其中的 `.vcd` 文件。 + +接下来再看看 Testbench 是怎么写的。 + +```cpp +#include "VAdder_1.h" +#include "verilated.h" +#include "verilated_vcd_c.h" + +#include +#include + +#define MAX_SIM_TIME 300 +vluint64_t main_time = 0; + +void poke(VAdder_1 *topp) +{ + topp->A = rand() % 2; + topp->B = rand() % 2; + topp->CI = rand() % 2; + printf("[poke] S(?) CO(?) A(%x) B(%x) CI(%x)\n", topp->A, topp->B, topp->CI); +} + +bool peek_and_check(VAdder_1 *topp) +{ + printf("[peek] S(%x) CO(%x) A(%x) B(%x) CI(%x)\n", topp->S, topp->CO, topp->A, topp->B, topp->CI); + return (topp->S == topp->A ^ topp->B ^ topp->CI) && (topp->CO == (topp->A & topp->B) | (topp->A & topp->CI) | (topp->B & topp->CI)); +} + +int main(int argc, char **argv, char **env) +{ + Verilated::commandArgs(argc, argv); + VAdder_1 *topp = new VAdder_1; + + Verilated::traceEverOn(true); + VerilatedVcdC *tfp = new VerilatedVcdC; + topp->trace(tfp, 99); + tfp->open("VAdder_1.vcd"); + + while (main_time < MAX_SIM_TIME && !Verilated::gotFinish()) + { + printf("[time] %ld\n", main_time); + poke(topp); + topp->eval(); + tfp->dump(main_time); + if (!peek_and_check(topp)) + { + printf("Verification failed!\n"); + break; + } + main_time++; + } + + tfp->close(); + delete topp; + exit(0); +} +``` + +可以看到,我们编写的 Verilog Module 被转换为 C++ 中的一个类。我们将在 `main` 中实例化该 Module,并使用自己编写的 `poke` 和 `peek_and_check` 函数对其进行测试。其中 `poke` 用于随机生成输入,`peek_and_check` 用于检查输出是否正确。可以看到,类内的成员均是我们定义的 Label。`peek_and_ckeck` 函数手写了正确的逻辑表达式,并返回与 Module 输出的比较结果。Testbench 的编写还是比较容易理解的。 ## 编程模型 在书中展示了几种 Verilog 编程方式: -- 逻辑功能描述:使用 `if` 等逻辑预计描述该模块的功能 -- 内部连线描述:使用 `wire` 定义模块内部的连线 +- 逻辑功能描述:使用 `if` 等逻辑语句描述该模块的功能 +- 内部连线描述:使用 `wire` 定义模块内部的连线,使用 `assign` 连接连线和逻辑 - 原语描述:使用 `and`、`or` 等原语描述模块的功能 数逻教材中的大部分 Verilog 都采用后两种编程方式结合。 -## \ No newline at end of file +## 模块的结构、数据类型、变量和基本运算符号 + +### 模块 + +两个部分,I/O 接口和逻辑功能。 + +基本模块结构如下: + +```verilog +module block(a, b, c, d) + input a, b; + output c, d; + + wire e, f; + assign e = a & b; + assign f = a | b; + assign c = e ^ f; + assign d = e & f; +endmodule +``` + +- `block` 是模块名,后面是输入输出端口 +- `input` 和 `output` 关键字的部分是 I/O 说明,它也可以写在端口声明语句,如: + +```verilog +module block(input a, input b, output c, output d) +``` + +接下来就是内部信号说明(`wire` 和 `reg` 等的声明)和功能定义(逻辑部分) + +### 功能定义 + +有 3 种方式能够产生逻辑: + +- `assign` 声明语句 +- 实例元件 `and #2 ul(a, b, c);` +- `always` 块 + +```verilog +always @ (a or b) + c = a & b; +``` + +`assign` 最常用,`always` 既可描述组合逻辑,也可描述时序逻辑,手段较多。 + + +!!! warning "并行性" + + 如果把上面三个部分放到一个模块中,它们之间是并行的。它们之间的顺序不会影响实现的功能。 + + `always` 模块内,语句是顺序执行的。 + + +### 使用模块 + +有两种方式: + +- 严格按照定义的端口顺序来连接。 + +```verilog +block MyBlock(a, b, c, d); +``` + +- 用 `.portname(signal)` 来连接: + +```verilog +block MyBlock(.a(a), .b(b), .c(c), .d(d)); +``` + +第二种方式不必按端口顺序对应,提高可读性和可移植性。 + +### 数据类型 + +共 19 种,这里介绍 4 种。 + +#### 常量-数字 + +#### 常量-`parameter` 类型 + +#### 变量 + +#### 运算符和表达式 + diff --git a/docs/links.md b/docs/links.md index ac68a3e1..5e45cec0 100644 --- a/docs/links.md +++ b/docs/links.md @@ -52,6 +52,16 @@ glightbox: False + +