ICS PA实验课

Tips

  1. 使用反向代理时,git相关命令会出问题
  2. 实验前,创建新分支pa号,在其中做修改,完成以后合并pa到master中,并转回master分支

模板

# PA
## notification
-
## difficulties
1.
## summary

PA0

notification

  • 全程使用git
  • git commit --allow-empty可以使git空(无更改的)提交

difficulties

  1. 第一个坑,make menuconfig时提示
    Makefile:18: *** NEMU_HOME= is not a NEMU repo.  Stop.   

原因:用了tmux分割窗口,上窗口source .bashrc更新了,下窗口没有,所以$NEMU_HOME没有定义
2. 运行make menuconfig提示

make[1]: bison: No such file or directory # 没有安装bison
make[1]: flex: No such file or directory # lack of 'flex'

summary

  • 今天,2024年2月9日,除夕,我终于完成了PA0。在这个过程中,我主要遇到了几个困难:
  1. VMware断网。先是弃用了Debian,装了ics2023指定的Ubuntu 22.04版本,才发现仍然没有网络,是VMware的问题,初始化网络后得以解决。
  2. 环境问题。Ubuntu desktop版apt-get使用有问题,换成了server版。死磕最小安装,最后在安装man的过程中不得不unminimize,联网下载了很久,聪明误把聪明误。看到课程要求必须使用GUI,又非常笨蛋地下回了desktop版;最后才想到使用server并安装gnome图形界面。
  3. ssh终端问题。Mobaxterm右对齐窗口溢出、无法调整窗口大小的bug,几经周折换成了Terminus。
  4. 反向代理问题。git clone过程中因为开启了github反向代理,一直被拒绝连接。
  5. 缺乏linux使用经验。看到nemu提示No such file or directory不知道是没有安装工具。
  • 最后我想说,我终于做到了。英语、按要求debug c程序、看懂命令行回显、搞懂git的使用方式,在接触ICS PA的215天后,我做到了。
  • Where there is a will, there is a wall. The wall only blocks those who don’t really want to get closer to their will. That is it.

To-do

  • Read the source code and the mannual, Search the web and AI, and Keep grit.

PA1

Ahead

2024/02/10 成功运行马里奥.nes

notification

  • 独立解决问题。即使是一个小bug,也能顺带学到许多
  • 多看篮框题
  • make ... -j2使用两个CPU编译
  • ccache:节省编译时间的工具。会把目标文件保存,即使make clean了也能保持非常快的编译速度(跳过了重复编译的过程,发挥加速作用)
  • NEMU:NES Emulator
  • “程序是代码”视角(整体):ICS PA中期会介绍,抽象了细节,更容易理解整个程序
  • “程序是状态机”视角(局部):ICS第三章专门分析状态机视角(单步执行程序,进入细节部分)
  • 阅读源代码:从main()开始。
  • kconfig:配置系统。make menuconfig时生成conf文件,解析kconfig,二者结合生成宏定义,存储在nemu/include/generated/autoconf.h, 阅读C代码时使用;nemu/include/config/auto.conf, 阅读Makefile时使用

difficulties

  1. 运行ALSA时Terminus的显示问题。
    Power on
    Initializing video...
    ALSA lib confmisc.c:855:(parse_card) cannot find card '0' # ALSA在Terminus中显示不出来
  2. 没有GUI界面,make ARCH=native run mainargs=mario显示如下:
    Power on
    Initializingyideo
    system time: 8s) FPS = 59
    System time: 9s) FPS = 59

最后安装了LXDE,过程中需要安装lightdm(相比gdm3更轻的显示管理器),用cat /etc/X11/default-display-manager命令查看当前使用的显示管理器
3. 磁盘爆了。/dev/mapper/ubuntu--vg-ubuntu--lv 9.8G 9.8G 0 100% /用完了分区的10G存储空间,扩展分区时也提示No space left on device,连拓展分区的命令都写不下了:),最后apt-get autoclean拯救了我,为我清除了0.8G的内容。
4. manual理解问题。配置ccache时,手册提示

To use the second method on a Debian system, it’s easiest to just prepend /usr/lib/ccache to your PATH.

但是我没有搞懂prepend yo your PATH的含义。(没想到就是添加到$PATH,字面意思,PATH不指代其他路径)最终AI帮了我:

# echo $PATH结果
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

# 修改~/.bashrc
export PATH="/usr/lib/ccache:$PATH"
# 双引号里的PATH是上面的旧的PATH

#echo $PATH结果
/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

可以看到在$PATH最前面加上了ccache(即PATH=ccache:$PATH(oringin)的含义)
附:[[…/笔记/Linux笔记#环境变量|常见环境变量]]
5. 不知道如何使用grep命令查找。不知道main函数在哪,找到main函数了不知道main执行的函数在哪。[[…/笔记/Linux笔记#^0608f3|grep教程]]

RTFS

to-do

[ ] 记得学习gdb调试,并且修正直接退出的makefile bug

notification

  • cmd_c()函数中, 调用cpu_exec()的时候传入了参数-1, 你知道这是什么意思吗?
 74 static void execute(uint64_t n) { // 无符号型!
75 Decode s;
76 for (;n > 0; n--) { // 传入-1则永远n<0(错误)
... ...

意味着无限循环执行(错了,n是无符号整数,被解释成最大的数,也就是说会终止)

  • 在程序设计课上老师告诉你, 当程序执行到main()函数返回处的时候, 程序就退出了, 你对此深信不疑. 但你是否怀疑过, 凭什么程序执行到main()函数的返回处就结束了? 如果有人告诉你, 程序设计课上老师的说法是错的, 你有办法来证明/反驳吗? 如果你对此感兴趣, 请在互联网上搜索相关内容。
    C99标准:

5.1.2.2.3 Program termination
If the return type of the main function is a type compatible with int, a return from the
initial call to the main function is equivalent to calling the exit function with the value
returned by the main function as its argument;11) reaching the } that terminates the
main function returns a value of 0. If the return type is not compatible with int, the
termination status returned to the host environment is unspecified.
Forward references: definition of terms (7.1.1), the exit function (7.22.4.4).

  • 为了测试大家是否已经理解框架代码, 我们给大家设置一个练习: 如果在运行NEMU之后直接键入q退出, 你会发现终端输出了一些错误信息. 请分析这个错误信息是什么原因造成的, 然后尝试在NEMU中修复它.

difficulties

  1. 不知道sdb_mainloop()怎么就跳到cpu_exec()去了。vim搜索后发现在cmd_c函数里哈哈
  2. 学会了直接用:e folder/file:bn/bp直接在vim里打开和切换文件

summary