1. 生成公钥和私钥

    Windows

    在桌面,键盘按win键+r键,输入cmd,回车,打开cmd终端输入:

    1
    ssh-keygen -t rsa

    一路回车

    Linux

    打开终端,输入:

    1
    ssh-keygen -t rsa

    一路回车

  2. 复制刚刚生成的公钥

    Windows

    在桌面,键盘按win键+r键,输入cmd,回车,打开cmd终端输入:

    1
    notepad %HOMEPATH%/.ssh/id_rsa.pub

    复制这一行

    Linux

    打开终端,输入:

    1
    cat ~/.ssh/id_rsa.pub

    复制输出的结果

[!注意]

这一行很长!一定要复制完。格式以ssh-rsa xxxxxxxx开头,以你的用户名@你的系统名结尾,如:

1
ssh-rsa AAAA......xxxx= satan\satan@SatanGT
  1. 将本地的公钥复制到服务器上

    • 通用方法

      登录linux后,先检查有没有~/.ssh/authorized_keys文件,如果没有,输入:

      1
      2
      3
      4
      mkdir ~/.ssh
      touch ~/.ssh/authorized_keys
      chmod 700 ~/.ssh
      chmod 600 ~/.ssh/authorized_keys

      将刚才复制的公钥粘贴到authorized_keys里。

      或输入:

      1
      echo ssh-rsa AAAA......xxxx= user@host(上面复制的公钥) >> ~/.ssh/authorized_keys
    • Linux快捷指令
      打开终端,输入:

      1
      2
      3
      4
      # 将user@remote-server换成你的服务器,可以带端口等选项
      ssh-copy-id user@remote-server
      # 或
      ssh-copy-id -i ~/.ssh/work_key.pub -p 2222 user@remote-server

​ 至此,后续的ssh可以免密登录。


  1. 常用的指令

    • tab补全忽略大小写
    1
    echo 'set completion-ignore-case on' >> ~/.inputrc
    • .bashrc

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      case $- in
      *i*) ;;
      *) return;;
      esac

      if [ -f ~/.bash_aliases ]; then
      . ~/.bash_aliases
      fi

      # enable programmable completion features (you don't need to enable
      # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
      # sources /etc/bash.bashrc).
      if ! shopt -oq posix; then
      if [ -f /usr/share/bash-completion/bash_completion ]; then
      . /usr/share/bash-completion/bash_completion
      elif [ -f /etc/bash_completion ]; then
      . /etc/bash_completion
      fi
      fi

      if [ -e $HOME/.bash_functions ]; then
      source $HOME/.bash_functions
      fi

    • .bash_functions

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      function cd() {
      DIR="$*";
      # if no DIR given, go home
      if [ $# -lt 1 ]; then
      DIR=$HOME;
      fi;
      builtin cd "${DIR}" && \
      # use your preferred ls command
      ls -F --color=auto
      }
    • .bash_aliases

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      alias gh='history|grep'
      alias cpv='rsync -ah --info=progress2'
      alias ..='cd ..'
      alias ...='cd ../..'
      alias untar='tar -zxvf '
      alias pg='ps -aux|grep '
      woc_he() {
      curl -s "https://ipinfo.io/${1:-}"
      echo
      }
    • 检测本机ip

      1
      2
      curl ipinfo.io
      curl ifconfig.me
    • 检测他人ip

      1
      curl ipinfo.io/[ip]

1. 基本语法:

1
2
3
if [ command ]; then
符合该条件执行的语句
fi

2. 扩展语法:

1
2
3
4
5
6
7
if [ command ];then
符合该条件执行的语句
elif [ command ];then
符合该条件执行的语句
else
符合该条件执行的语句
fi

3. 语法说明:

​ bash shell会按顺序执行if语句,如果command执行后且它的返回状态是0,则会执行符合该条件执行的语句,否则后面的命令不执行,跳到下一条命令。
当有多个嵌套时,只有第一个返回0退出状态的命令会导致符合该条件执行的语句部分被执行,如果所有的语句的执行状态都不为0,则执行else中语句。
返回状态:最后一个命令的退出状态,或者当没有条件是真的话为0。

注意:

  1. [ ]表示条件测试。注意这里的空格很重要。要注意在[后面和]前面都必须要有空格
  2. 在shell中,thenfi是分开的语句。如果要在同一行里面输入,则需要用分号将他们隔开。
  3. 注意if判断中对于变量的处理,需要加引号,以免一些不必要的错误。没有加双引号会在一些含空格等的字符串变量判断的时候产生错误。比如[ -n "$var" ]如果var为空会出错
  4. 判断是不支持浮点值的
  5. 如果只单独使用>或者<号,系统会认为是输出或者输入重定向,虽然结果显示正确,但是其实是错误的,因此要对这些符号进行转意
  6. 在默认中,运行if语句中的命令所产生的错误信息仍然出现在脚本的输出结果中
  7. 使用-z或者-n来检查长度的时候,没有定义的变量也为0
  8. 空变量和没有初始化的变量可能会对shell脚本测试产生灾难性的影响,因此在不确定变量的内容的时候,在测试号前使用-n或者-z测试一下
  9. ? 变量包含了之前执行命令的退出状态(最近完成的前台进程)(可以用于检测退出状态)

4. 常用参数:

文件/目录判断:

常用的:

1
2
3
4
5
6
7
[ -a FILE ] 如果 FILE 存在则为真。
[ -d FILE ] 如果 FILE 存在且是一个目录则返回为真。
[ -e FILE ] 如果 指定的文件或目录存在时返回为真。
[ -f FILE ] 如果 FILE 存在且是一个普通文件则返回为真。
[ -r FILE ] 如果 FILE 存在且是可读的则返回为真。
[ -w FILE ] 如果 FILE 存在且是可写的则返回为真。(一个目录为了它的内容被访问必然是可执行的)
[ -x FILE ] 如果 FILE 存在且是可执行的则返回为真。

不常用的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[ -b FILE ] 如果 FILE 存在且是一个块文件则返回为真。
[ -c FILE ] 如果 FILE 存在且是一个字符文件则返回为真。
[ -g FILE ] 如果 FILE 存在且设置了SGID则返回为真。
[ -h FILE ] 如果 FILE 存在且是一个符号符号链接文件则返回为真。(该选项在一些老系统上无效)
[ -k FILE ] 如果 FILE 存在且已经设置了冒险位则返回为真。
[ -p FILE ] 如果 FILE 存并且是命令管道时返回为真。
[ -s FILE ] 如果 FILE 存在且大小非0时为真则返回为真。
[ -u FILE ] 如果 FILE 存在且设置了SUID位时返回为真。
[ -O FILE ] 如果 FILE 存在且属有效用户ID则返回为真。
[ -G FILE ] 如果 FILE 存在且默认组为当前组则返回为真。(只检查系统默认组)
[ -L FILE ] 如果 FILE 存在且是一个符号连接则返回为真。
[ -N FILE ] 如果 FILE 存在 and has been mod如果ied since it was last read则返回为真。
[ -S FILE ] 如果 FILE 存在且是一个套接字则返回为真。
[ FILE1 -nt FILE2 ] 如果 FILE1 比 FILE2 新, 或者 FILE1 存在但是 FILE2 不存在则返回为真。
[ FILE1 -ot FILE2 ] 如果 FILE1 比 FILE2 老, 或者 FILE2 存在但是 FILE1 不存在则返回为真。
[ FILE1 -ef FILE2 ] 如果 FILE1 和 FILE2 指向相同的设备和节点号则返回为真。

字符串判断

1
2
3
4
5
6
7
[ -z STRING ] 如果STRING的长度为零则返回为真,即空是真
[ -n STRING ] 如果STRING的长度非零则返回为真,即非空是真
[ STRING1 ]  如果字符串不为空则返回为真,与-n类似
[ STRING1 == STRING2 ] 如果两个字符串相同则返回为真
[ STRING1 != STRING2 ] 如果字符串不相同则返回为真
[ STRING1 < STRING2 ] 如果 “STRING1”字典排序在“STRING2”前面则返回为真。
[ STRING1 > STRING2 ] 如果 “STRING1”字典排序在“STRING2”后面则返回为真。

数值判断

1
2
3
4
5
6
[ INT1 -eq INT2 ] INT1和INT2两数相等返回为真 ,=
[ INT1 -ne INT2 ] INT1和INT2两数不等返回为真 ,<>
[ INT1 -gt INT2 ] INT1大于INT2返回为真 ,>
[ INT1 -ge INT2 ] INT1大于等于INT2返回为真,>=
[ INT1 -lt INT2 ] INT1小于INT2返回为真 ,<
[ INT1 -le INT2 ] INT1小于等于INT2返回为真,<=

逻辑判断

1
2
3
4
5
[ ! EXPR ] 逻辑非,如果 EXPR 是false则返回为真。
[ EXPR1 -a EXPR2 ] 逻辑与,如果 EXPR1 and EXPR2 全真则返回为真。
[ EXPR1 -o EXPR2 ] 逻辑或,如果 EXPR1 或者 EXPR2 为真则返回为真。
[ ] || [ ] 用OR来合并两个条件
[ ] && [ ] 用AND来合并两个条件

其他判断

1
2
[ -t FD ] 如果文件描述符 FD (默认值为1)打开且指向一个终端则返回为真
[ -o optionname ] 如果shell选项optionname开启则返回为真

5. IF高级特性:

​ 双圆括号(( )):表示数学表达式
​ 在判断命令中只允许在比较中进行简单的算术操作,而双圆括号提供更多的数学符号,而且 在双圆括号里面的><号不需要转意。

​ 双方括号[[ ]]:表示高级字符串处理函数
​ 双方括号中判断命令使用标准的字符串比较,还可以使用匹配模式,从而定义与字符串相匹配的正则表达式。

6. 双括号的作用:

​ 在shell中,[ $a != 1 || $b = 2 ]是不允许出,要用[ $a != 1 ] || [ $b = 2 ],而双括号就可以解决这个问题的,[[ $a != 1 || $b = 2 ]]。又比如这个[ "$a" -lt "$b" ],也可以改成双括号的形式(("$a" < "$b"))

7. Example

1. 判断目录$doiido是否存在,若不存在,则新建一个

1
2
3
if [ ! -d "$doiido"]; then
  mkdir "$doiido"
fi

2.判断普通文件$doiido是否存在,若不存在,则新建一个

1
2
3
if [ ! -f "$doiido" ]; then
  touch "$doiido"
fi

3.判断$doiido是否存在并且是否具有可执行权限

1
2
3
4
if [ ! -x "$doiido"]; then
  mkdir "$doiido"
chmod +x "$doiido"
fi

4.是判断变量$doiido是否有值

1
2
3
4
if [ ! -n "$doiido" ]; then
  echo "$doiido is empty"
  exit 0
fi

5.两个变量判断是否相等

1
2
3
4
5
if [ "$var1" = "$var2" ]; then
  echo '$var1 eq $var2'
else
  echo '$var1 not eq $var2'
fi

6.测试退出状态:

1
2
3
if [ $? -eq 0 ];then
echo 'That is ok'
fi

7.数值的比较:

1
2
3
if [ "$num" -gt "150" ];then
echo "$num is biger than 150"
fi

8.a>ba<c

1
2
3
(( a > b )) && (( a < c ))
[[ $a > $b ]] && [[ $a < $c ]]
[ $a -gt $b -a $a -lt $c ]

9.a>ba<c

1
2
3
(( a > b )) || (( a < c ))
[[ $a > $b ]] || [[ $a < $c ]]
[ $a -gt $b -o $a -lt $c ]

10.检测执行脚本的用户

1
2
3
4
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user."
exit 1;
fi

上面的语句也可以使用以下的精简语句

1
[ "$(whoami)" != 'root' ] && ( echo "You have no permission to run $0 as non-root user."; exit 1 )

11.正则表达式

1
2
3
4
doiido="hero"
if [[ "$doiido" == h* ]];then
echo "hello,hero"
fi

8. ===其他例子===

1. 查看当前操作系统类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh

SYSTEM=`uname -s`
if [ $SYSTEM = "Linux" ] ; then
echo "Linux"
elif
[ $SYSTEM = "FreeBSD" ] ; then
echo "FreeBSD"
elif
[ $SYSTEM = "Solaris" ] ; then
echo "Solaris"
else
echo "What?"
fi

2. if利用read传参判断

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
read -p "please input a score:" score
echo -e "your score [$score] is judging by sys now"
if [ "$score" -ge "0" ]&&[ "$score" -lt "60" ];then
echo "sorry,you are lost!"
elif [ "$score" -ge "60" ]&&[ "$score" -lt "85" ];then
echo "just soso!"
elif [ "$score" -le "100" ]&&[ "$score" -ge "85" ];then
echo "good job!"
else
echo "input score is wrong , the range is [0-100]!"
fi

3. 判断文件是否存在

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh
today=`date -d yesterday +%y%m%d`
file="apache_$today.tar.gz"
cd /home/chenshuo/shell

if [ -f "$file" ];then
echo “”OK"
else
echo "error $file" >error.log
mail -s "fail backup from test" loveyasxn924@126.com <error.log
fi

4. 这个脚本在每个星期天由cron来执行。如果星期的数是偶数,他就提醒你把垃圾箱清理:

1
2
3
4
5
#!/bin/bash
WEEKOFFSET=$[ $(date +"%V") % 2 ]
if [ $WEEKOFFSET -eq "0" ]; then
echo "Sunday evening, put out the garbage cans." | mail -s "Garbage cans out" your@your_domain.org
fi

5. 挂载硬盘脚本(windows下的ntfs格式硬盘)

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
#! /bin/sh
dir_d=/media/disk_d
dir_e=/media/disk_e
dir_f=/media/disk_f
a=`ls $dir_d | wc -l`
b=`ls $dir_e | wc -l`
c=`ls $dir_f | wc -l`
echo "checking disk_d..."
if [ $a -eq 0 ]; then
echo "disk_d is not exsit,now creating..."
sudo mount -t ntfs /dev/disk/by-label/software /media/disk_d
else
echo "disk_d exits"
fi
echo "checking disk_e..."
if [ $b -eq 0 ]; then
echo "disk_e is not exsit,now creating..."
sudo mount -t ntfs /dev/disk/by-label/elitor /media/disk_e
else
echo "disk_e exits"
fi
echo "checking disk_f..."
if [ $c -eq 0 ]; then
echo "disk_f is not exsit,now creating..."
sudo mount -t ntfs /dev/disk/by-label/work /media/disk_f
else
echo "disk_f exits"
fi

1. Introduction:

  • Module Name: ahb_sfr

  • Module Overview: The ahb_sfr module is designed to interface with a system’s Special Function Registers (SFRs) via the AHB (Advanced High-performance Bus). It primarily handles the reading and writing operations to these registers based on external requests. The module synchronizes these requests and manages data transfer between the external bus and the internal registers, ensuring data integrity and proper timing.

  • Timing control:

2. Input/Output Interfaces Descriptions:

  • Inputs:

    • sfrclk: The clock signal for the module. All synchronous operations are triggered on the rising edge of this clock.
    • sfrrstz: Active low reset signal. When asserted, it initializes the module’s internal registers and logic.
    • sfrdatai [7:0]: Data input from the SFRs to be sent to the external bus.
    • ADDR [14:0]: Address bus input specifying the SFR address for the current operation.
    • DATA [7:0]: Data bus input specifying the data to be written to the SFRs.
    • REQ: Request signal indicating an active read or write operation.
    • WR: Write enable signal indicating if the current operation is a write (asserted) or read (deasserted).
  • Outputs:

    • sfrwe: Write enable output that signals whether a write operation should be performed to the SFRs.
    • sfraddr (output wire [14:0]): Address output that mirrors the input address to the SFRs.
    • sfrdatao [7:0]: Data output holding the data to be written to the SFRs.
    • s2adata [7:0]: Data output that directly mirrors the input sfrdatai, intended for external monitoring or further processing.
    • ACK: Acknowledge signal toggled to indicate the completion of a request processing cycle.

3. Clock and Reset Strategy:

  • Clock:

    • Name: sfrclk
    • Active State: The clock signal is active on the rising edge, which is used to synchronize all sequential logic within the module.
  • Reset:

    • Name: sfrrstz
    • Active State: The reset signal is active-low. When asserted, it initializes the module’s internal registers and logic.

4-1. Parameters constant:

  • None

4-2. Macro constant:

  • None

5. Algorithmic Logic

  • Reset and Initialization:
    All internal registers (ReqSyncD, SFRWrS, data_i, wr_i, addr_i, sfrwe, sfrdatao, and ACK) are initialized to their default states upon reset (sfrrstz asserted).

  • Request Synchronization:
    The module uses a simple synchronization mechanism to detect edges in the REQ signal. This is achieved by comparing the current REQ state with a delayed version (ReqSyncD), stored from the previous clock cycle. The result (ReqToggle) indicates a change in the request state, triggering data and control updates.

  • Data and Control Flow:
    Upon detecting a request (ReqToggle asserted), the module captures the address (ADDR), data (DATA), and write enable (WR) from the inputs and stores them in internal registers (addr_i, data_i, wr_i). These values are used to set up the subsequent operations.

    • If a write operation is detected (wr_i asserted), the write enable output (sfrwe) is set based on the SFRWrS status, which tracks the request state to prevent erroneous writes during request transitions.
    • For write operations, the data to be written to the SFR (sfrdatao) is updated with the value from data_i if both wr_i and SFRWrS are asserted.
  • Output and Acknowledgement:
    The address output (sfraddr) directly mirrors the internal address register (addr_i). The s2adata output is a direct pass-through of the sfrdatai input, allowing external entities to monitor or process the incoming SFR data. The ACK signal is toggled to indicate the completion of a processing cycle, helping external controllers to manage the timing and sequence of operations.

6 Operational Cycles

The ahb_sfr module is designed to interface with a system bus, handling specific register operations including data transfer and synchronization. The module operates primarily on the rising edge of the system clock (sfrclk) and remains sensitive to the active low reset signal (sfrrstz). The core functionality revolves around capturing and responding to data requests from the bus, managing data write operations, and acknowledging the completion of these operations.

Sequential Logic:

  • Reset and Synchronization: The module uses edge-triggered flip-flops to capture and synchronize the request signal (REQ). This synchronization helps in mitigating any metastability issues due to the asynchronous nature of the input request.
  • Data and Control Signal Capturing: Upon detecting a change in the request signal (ReqToggle), the module captures the address (ADDR), data (DATA), and write control signal (WR) from the bus. These captured values are stored in internal registers (addr_i, data_i, wr_i) and are used in subsequent operations.
  • Write Enable Logic: The write enable signal (sfrwe) is controlled based on the write request (wr_i) and the synchronization status (SFRWrS). This ensures that write operations are only enabled under valid conditions.
  • Data Output and Acknowledgment: The module outputs data (sfrdatao) and toggles the acknowledgment signal (ACK) based on the internal state and the synchronization of the request.

Combinational Logic:

  • Request Toggle Detection: A simple XOR gate detects changes in the request signal, generating a toggle signal (ReqToggle) that triggers data capturing and acknowledgment logic.
  • Data Output to System Bus: The output data to the system bus (s2adata) is directly driven by the input data from another source (sfrdatai), indicating a simple combinational pathway.

7. Data Flow

  • ReqSyncD (Request Synchronized Delayed): This signal holds the delayed version of the external request signal (REQ). It is used to detect edges in the request signal by comparing its current state to its previous state.
  • data_i (Data Input Register): Captures the data from the bus when a new request is detected. This register temporarily holds the data for processing or forwarding during the write operations.
  • SFRWrS (SFR Write Synchronize): Indicates the synchronization status for write operations. It is set when a new request is detected and used to control the write enable signal.
  • wr_i (Write Input Register): Captures the write control signal from the bus, indicating whether the current operation involves writing data to the register.
  • addr_i (Address Input Register): Holds the address from the bus where data needs to be written or read, captured upon a new request.
  • ReqToggle (Request Toggle): A signal generated by XORing the current and delayed request signals. It indicates a change in the request status, used to trigger data capturing and acknowledgment logic.
  • sfrwe (SFR Write Enable): Controlled by the write request and synchronization status, this signal enables the data write operation to the internal registers.
  • sfrdatao (SFR Data Output): Outputs data based on the internal logic conditions, specifically during write operations when both write request and synchronization are affirmed.
  • ACK (Acknowledgment): Toggled in response to a new request detection, signaling the completion of a data read or write operation.

1. Introduction:

  • Module Name: ct_pmp_regs

  • Module Overview: The ct_pmp_regs module is designed to manage and configure a set of registers related to physical memory protection (PMP) in a processor. The primary objective of this module is to handle the read and write operations to PMP configuration and address registers. It ensures that the PMP settings are correctly updated based on control signals, allowing for secure and controlled access to memory regions. The module supports multiple PMP entries, each with configurable attributes such as read, write, execute permissions, address mode, and lock status.

  • Timing control:

2. Input/Output Interfaces Descriptions:

  • Inputs:

    • cp0_pmp_wdata [63:0]: A 64-bit data bus used to write data into the PMP configuration and address registers.
    • cpuclk: The clock signal for synchronizing operations within the module.
    • cpurst_b: An active-low reset signal used to initialize the module’s registers to their default states.
    • pmp_csr_sel [17:0]: An 18-bit signal used to select which PMP configuration or address register is being accessed.
    • pmp_csr_wen [17:0]: An 18-bit write enable signal used to control the write operations to the PMP registers.
  • Outputs:

    • pmp_cp0_data [63:0]: A 64-bit data bus used to output data from the PMP configuration and address registers.
    • pmpaddr0_value [28:0] to pmpaddr7_value [28:0]: Eight 29-bit signals representing the values of the PMP address registers.
    • pmpcfg0_value [63:0]: A 64-bit signal representing the combined values of the first four PMP configuration registers.
    • pmpcfg2_value [63:0]: A 64-bit signal representing the combined values of the next four PMP configuration registers.

3. Clock and Reset Strategy:

  • Clock:

    • Name: cpuclk
    • Active State: The clock signal is active on the rising edge, which is used to synchronize all sequential logic within the module.
  • Reset:

    • Name: cpurst_b
    • Active State: The reset signal is active-low. When asserted (logic 0), it initializes all PMP configuration and address registers to their default states, ensuring a known starting condition for the module.

4-1. Parameters constant:

The module contains the following parameter constant:

  • parameter ADDR_WIDTH = 29;

4-2. Macro constant:

  • None

5. Algorithmic Logic

  1. Configuration Registers:

    • The module contains multiple configuration registers (pmpcfg) which store permissions and attributes such as readable, writable, executable, and locked states.
    • Each configuration register manages separate regions or segments of memory, with specific fields determining the operation mode and permissions of each segment.
  2. PMP Initialization:

    • Upon reset (cpurst_b signal low), all the configuration and address registers within the module are initialized to a predefined state (0), ensuring a secure and known initial state.
  3. Permission and Mode Settings:

    • Each PMP configuration register (pmpcfgX) holds bits that define access rights: readable, writable, and executable, alongside a two-bit mode for address matching.
    • The lock bit in each configuration register indicates whether further writes to the register are possible.
  4. Hardware Updates:

    • Configuration registers are updated on the rising edge of the clock (cpuclk) when the corresponding write enable (pmp_csr_wen) signals are valid and no lock conditions are met.
    • Address registers are updated similarly, respecting any lock and match-mode dependencies ensuring secure modification prevention.
  5. Chaining Logic:

    • Address matching and permission interpretation rely on the state of the configuration registers, such as mode matching and lock conditions.
    • For chained configurations, the write operations to a PMP address are conditional on already established locks and address mode values.
  6. Inter-module Communication:

    • Input cp0_pmp_wdata carries the data for register updates, while pmp_cp0_data serves as an output conveying PMP configurations to other modules.
    • Selection and enable vectors (pmp_csr_sel, pmp_csr_wen) control which registers are accessed for read/write operations.

6 Operational Cycles

  • Reset Phase: Registers are reset, meaning they all initialize to zero, ensuring that all regions are initially inaccessible until explicitly configured.

  • Configuration Phase: During normal operation post-reset, configuration registers can be updated based on provided data (cp0_pmp_wdata) when writing is enabled via control signals and existing register contents permit the modification (e.g., no active lock bits).

  • Normal Operation: During normal runtime, the PMP registers govern access permissions dynamically, allowing secure and managed access to the processor’s memory space per the configuration stored within these registers.
    Algorithm Mechanics:

  • Registers: The module contains various configuration registers (pmp0cfg, pmp1cfg, … , pmp7cfg) and address registers (pmpaddr0_value through pmpaddr7_value) to manage PMP regions.

  • Configuration Logic:

    • Each pmpcfg# register has fields for address matching mode, permissions (readable, writable, executable), and a lock bit to prevent further modifications.
    • Data from cp0_pmp_wdata is loaded into registers based on write enable signals (pmp_csr_wen[]) provided that the lock bit is not set.
  • Address Logic:

    • Address registers are updated based on corresponding enable signals ensuring the previous entry is not locked.

Timing: Updates to the registers are synchronized with the clock signal. On a rising edge of cpuclk, updates occur if the write enable (pmp_csr_wen[]) is set and the reset (cpurst_b) is not asserted.

7. Data Flow

Data Tracing and Processing:

  • Input to Register Update: Data from cp0_pmp_wdata is stored into the appropriate configuration (pmpcfg#) or address (pmpaddr#_value) register based on the current state of selection and enable signals, considering lock conditions.
  • Configuration to Output: The corresponding configuration or address states can be read out via pmp_cp0_data or pmpcfg0_value, based on pmp_csr_sel[].

Error Handling:

  • Lock Protection: If a lock bit within a configuration register is set, further writes to that configuration are blocked regardless of the enable state.

Timing in Data Movement:

  • Sequential Updates: Data from cp0_pmp_wdata is sequentially applied across cycles, starting with the first enabled register according to the selection and enable signals.

Concurrency Management:

  • Independent Configuration Blocks: Each pmpcfg# and pmpaddr# register set operates independently, allowing updates to different regions concurrently, as long as they adhere to lock and enable conditions.


1 Spec ————A 32-bits Multiplier


Input

Signal Bits Function
clk 1 Clock
mult_begin 1 begin multiply signal
mult_op1 32 32bits multiplier
mult_op2 32 32bits multiplier

Output

Signal Bits Function
product 64 multiply product
mult_end 1 end multiply signal

2 Design


2.1 testbench.v

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
`timescale 1ns / 1ps
`define cycles 40
module tb;

// Inputs
reg clk;
reg mult_begin;
reg [31:0] mult_op1;
reg [31:0] mult_op2;
reg [63:0] Data_in_t;
// Outputs
wire [63:0] product;
wire mult_end;

// Instantiate the Unit Under Test (UUT)
multiply uut (
.clk(clk),
.mult_begin(mult_begin),
.mult_op1(mult_op1),
.mult_op2(mult_op2),
.product(product),
.mult_end(mult_end)
);
integer i,errors[0:39],cnt;

integer fd = 0;
initial begin
// Initialize Inputs
clk = 0;
mult_begin = 0;
mult_op1 = 0;
mult_op2 = 0;
for (i = 0; i < 40; i = i + 1)
begin
errors[i] = 0;
end
i=0;
cnt=0;
$dumpfile("mul.vcd");

$dumpvars();
fd = $fopen("./report.txt", "w");
// if(!fd)
// begin
// $display("Could not open File \r");
// $stop;
// end

#1000;
//正数*正数
mult_begin = 1;
mult_op1 = 32'H6A98F28D;
mult_op2 = 32'H7E184AD4;
Data_in_t = 64'H348164E09FFD9EC4;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H2B661ACE;
mult_op2 = 32'H6AE5B749;
Data_in_t = 64'H121F3881A38CE6BE;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H25BD7BEE;
mult_op2 = 32'H4ADDFFD9;
Data_in_t = 64'H0B09801E84861EBE;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H0D3A404E;
mult_op2 = 32'H64B23883;
Data_in_t = 64'H0533F68AB11BF7EA;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H3F64172B;
mult_op2 = 32'H60CE4AB1;
Data_in_t = 64'H17F89DB9878072BB;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H16761A51;
mult_op2 = 32'H13462A55;
Data_in_t = 64'H01B0EBF60AAE06E5;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H2026D659;
mult_op2 = 32'H6D726BE8;
Data_in_t = 64'H0DBEE81CB76B73A8;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H02618586;
mult_op2 = 32'H1E83A59C;
Data_in_t = 64'H0048A717560EBBA8;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H1B77F796;
mult_op2 = 32'H5B4B942D;
Data_in_t = 64'H09CBC10E0A2B3D5E;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H5AC08456;
mult_op2 = 32'H64ED2634;
Data_in_t = 64'H23C745771E5DA578;
#50;
mult_begin = 0;
#1000;
//负数*负数
mult_begin = 1;
mult_op1 = 32'HDC7F53A9;
mult_op2 = 32'H9F5CEA89;
Data_in_t = 64'H0D66DE886A583F71;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'HB7AC2234;
mult_op2 = 32'HD3FCAAC7;
Data_in_t = 64'H0C6F5B2E9CB51E6C;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'HDAE39537;
mult_op2 = 32'HD347F65B;
Data_in_t = 64'H067B902D3789E48D;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H9F1D7AEA;
mult_op2 = 32'HFA3E1FC8;
Data_in_t = 64'H022DCC3B29965CD0;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H9F09D52B;
mult_op2 = 32'H99A59337;
Data_in_t = 64'H26C454CFE83B7D3D;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H90E87B84;
mult_op2 = 32'HEAA39889;
Data_in_t = 64'H09450737E2CC79A4;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'HEA722216;
mult_op2 = 32'HF7B63872;
Data_in_t = 64'H00B2A530D3EBFDCC;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'HB25BF9E7;
mult_op2 = 32'H9EAEE22C;
Data_in_t = 64'H1D83C041476EE1B4;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'HE23F69BC;
mult_op2 = 32'H804F5678;
Data_in_t = 64'H0ED712A6FC42B820;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H81A763DE;
mult_op2 = 32'HE857F5F4;
Data_in_t = 64'H0BACE522E690A598;
#50;
mult_begin = 0;
#1000;
//正数*负数
mult_begin = 1;
mult_op1 = 32'H117B72B5;
mult_op2 = 32'HFDE88F42;
Data_in_t = 64'HFFDB6F504BEEADAA;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H2EF80EBF;
mult_op2 = 32'HDE0A0A5F;
Data_in_t = 64'HF9C4E5A25416EEE1;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H56FA268C;
mult_op2 = 32'H86F6EBA9;
Data_in_t = 64'HD6E0AE135F0DF66C;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H77A17497;
mult_op2 = 32'HD367D395;
Data_in_t = 64'HEB29235711D250E3;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H5DE04686;
mult_op2 = 32'HD29727E2;
Data_in_t = 64'HEF59213D8FC6AC4C;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H0F50B083;
mult_op2 = 32'H844E2D5D;
Data_in_t = 64'HF89997CD13412697;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H671294E9;
mult_op2 = 32'HE6CCC285;
Data_in_t = 64'HF5DA8E00A12BEF0D;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H23B50CC7;
mult_op2 = 32'HCFD68C3B;
Data_in_t = 64'HF9484575D510C5DD;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H60F5B7D8;
mult_op2 = 32'HFA36D021;
Data_in_t = 64'HFDCF0059DC9C32D8;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H4E520906;
mult_op2 = 32'HC45F3B23;
Data_in_t = 64'HEDC1E86B8E859DD2;
#50;
mult_begin = 0;
#1000;
//0*随机
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'H51B64CC8;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'H7BA49B33;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'HF937655D;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'H72061A95;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'HA31321B7;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'H8532E4B1;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'HCC480B3E;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'HB712E17B;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'HFF3BAF67;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;
#1000;
mult_begin = 1;
mult_op1 = 32'H00000000;
mult_op2 = 32'H6D0E5A16;
Data_in_t = 64'H0000000000000000;
#50;
mult_begin = 0;

#500;
if (cnt == 0)
begin
$display("Simulation finished Successfully.");
$fdisplay(fd, "Simulation finished Successfully.");
end
else if (cnt >= 1)
begin
$display("%0d ERROR! See log for details.",cnt);
$fdisplay(fd, "%0d ERROR! See log above for details.",cnt) ;
end
$fclose(fd);

// $finish;
end
always #5 clk = ~clk;

//比较
always @ (negedge mult_end)
begin
if ( product !== Data_in_t || product == 64'Bz )
begin
// $display(" ------ERROR. A mismatch has occurred-----,ERROR in ", i);
$fdisplay(fd," ------ERROR. A mismatch has occurred-----,ERROR in ", i);
errors[i-1] = 1;
cnt = cnt + 1;
end
i = i + 1;

end
endmodule

2.2 updated_design_0.v

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

`timescale 1ns / 1ps

module multiply(
input clk, // Clock
input mult_begin, // begin multiply signal
input [31:0] mult_op1, // 32bits multiplier
input [31:0] mult_op2, // 32bits multiplier
output [63:0] product, // multiply product
output mult_end // end multiply signal
);
// Multiplication operation signals
reg mult_valid;
reg [31:0] multiplier;

assign mult_end = mult_valid & ~(|multiplier); // End signal: when multiplier is all zeros
always @(posedge clk)
begin
if (multiplier == 32'd0)
begin
mult_valid <= 1'b0; // No valid multiplication operation
end
else
begin
mult_valid <= 1'b1;
end
end

// Absolute values of operands
wire op1_sign; // Sign of operand 1
wire op2_sign; // Sign of operand 2
wire [31:0] op1_absolute; // Absolute value of operand 1
wire [31:0] op2_absolute; // Absolute value of operand 2
assign op1_sign = mult_op1[31];
assign op2_sign = mult_op2[31];
assign op1_absolute = op1_sign ? (~mult_op1+1) : mult_op1;
assign op2_absolute = op2_sign ? (~mult_op2+1) : mult_op2;

// Loading multiplicand and shifting
reg [63:0] multiplicand;
always @ (posedge clk)
begin
if (mult_valid)
begin // Shift multiplicand left by one bit each clock cycle
multiplicand <= {multiplicand[62:0],1'b0};
end
else if (mult_begin)
begin // Load multiplicand with absolute value of operand 1
multiplicand <= {32'd0,op1_absolute};
end
end

// Loading multiplier and shifting
always @ (posedge clk)
begin
if(mult_valid)
begin // Shift multiplier right by one bit each clock cycle
multiplier <= {1'b0,multiplier[31:1]};
end
else if(mult_begin)
begin // Load multiplier with absolute value of operand 2
multiplier <= op2_absolute;
end
end

// Partial product calculation
wire [63:0] partial_product;
assign partial_product = multiplier[0] ? multiplicand : 64'd0;

// Accumulator for the product
reg [63:0] product_temp;
always @ (posedge clk)
begin
if (mult_valid)
begin
product_temp <= product_temp + partial_product;
end
else if (mult_begin)
begin
product_temp <= 64'd0;
end
end

// Product sign and final result
reg product_sign; // Sign of the product
always @ (posedge clk)
begin
if (mult_valid)
begin
product_sign <= op1_sign ^ op2_sign; // Calculating sign of the product
end
end
assign product = product_sign ? (~product_temp+1) : product_temp; // Adjusting sign of the product
endmodule

3 Report


3.1 Compile Report

Errors: 0, Warnings: 0

1
2
3
4
5
6
7
8
9
10
Model Technology ModelSim SE-64 vlog 10.7 Compiler 2017.12 Dec  7 2017
Start time: 17:33:09 on Jan 26,2024
vlog -work work ./design/testbench.v ./design/updated_design_0.v -l vcompile.txt
-- Compiling module tb
-- Compiling module multiply

Top level modules:
tb
End time: 17:33:09 on Jan 26,2024, Elapsed time: 0:00:00
Errors: 0, Warnings: 0

3.2 Simulation Report

Errors: 0, Warnings: 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# vsim -voptargs="+acc" work.tb -l ./vsim.txt -wlf ./vsim.wlf 
# Start time: 17:33:09 on Jan 26,2024
# ** Note: (vsim-8009) Loading existing optimized design _opt2
# // ModelSim SE-64 10.7 Dec 7 2017
# //
# // Copyright 1991-2017 Mentor Graphics Corporation
# // All Rights Reserved.
# //
# // ModelSim SE-64 and its associated documentation contain trade
# // secrets and commercial or financial information that are the property of
# // Mentor Graphics Corporation and are privileged, confidential,
# // and exempt from disclosure under the Freedom of Information Act,
# // 5 U.S.C. Section 552. Furthermore, this information
# // is prohibited from disclosure under the Trade Secrets Act,
# // 18 U.S.C. Section 1905.
# //
# Loading work.tb(fast)
# Loading work.multiply(fast)
# Simulation finished Successfully.
# quit
# End time: 17:33:10 on Jan 26,2024, Elapsed time: 0:00:01
# Errors: 0, Warnings: 0

3.3 TestBench Report

1
Simulation finished Successfully.


1 Spec


左对齐 右对齐 居中对齐
单元格 单元格 单元格
单元格 单元格 单元格

2 Design


2.1 multiply.v

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
`timescale 1ns / 1ps

module multiply(
input clk, // 时钟
input mult_begin, // 乘法开始信号
input [31:0] mult_op1, // 乘法源操作数1
input [31:0] mult_op2, // 乘法源操作数2
output [63:0] product, // 乘积
output mult_end // 乘法结束信号
);
//乘法正在运算信号和结束信号
reg mult_valid;
reg [31:0] multiplier;

assign mult_end = mult_valid & ~(|multiplier); //乘法结束信号:乘数全0
endmodule

2.2 testbench.v

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
`timescale 1ns / 1ps
`define cycles 40
module tb;

// Inputs
reg clk;
reg mult_begin;
reg [31:0] mult_op1;
reg [31:0] mult_op2;
reg [63:0] Data_in_t;
// Outputs
wire [63:0] product;
wire mult_end;

// Instantiate the Unit Under Test (UUT)
multiply uut (
.clk(clk),
.mult_begin(mult_begin),
.mult_op1(mult_op1),
.mult_op2(mult_op2),
.product(product),
.mult_end(mult_end)
);
integer i,errors[0:39],cnt;
always #5 clk = ~clk;

endmodule

3 Log


3.1 Compile Log

Errors: 0, Warnings: 0

1
2
3
4
5
6
7
8
9
10
Model Technology ModelSim SE-64 vlog 10.7 Compiler 2017.12 Dec  7 2017
Start time: 20:07:49 on Dec 01,2023
vlog -work work ./design/multiply.v ./design/testbench.v -l vcompile.txt
-- Compiling module multiply
-- Compiling module tb

Top level modules:
tb
End time: 20:07:49 on Dec 01,2023, Elapsed time: 0:00:00
Errors: 0, Warnings: 0

3.2 Simulation Log

Errors: 0, Warnings: 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# vsim -voptargs="+acc" work.tb -l ./vsim.txt -wlf ./vsim.wlf 
# Start time: 20:07:49 on Dec 01,2023
# ** Note: (vsim-8009) Loading existing optimized design _opt2
# // ModelSim SE-64 10.7 Dec 7 2017
# //
# // Copyright 1991-2017 Mentor Graphics Corporation
# // All Rights Reserved.
# //
# // ModelSim SE-64 and its associated documentation contain trade
# // secrets and commercial or financial information that are the property of
# // Mentor Graphics Corporation and are privileged, confidential,
# // and exempt from disclosure under the Freedom of Information Act,
# // 5 U.S.C. Section 552. Furthermore, this information
# // is prohibited from disclosure under the Trade Secrets Act,
# // 18 U.S.C. Section 1905.
# //
# Loading work.tb(fast)
# Loading work.multiply(fast)
# ------ERROR. A mismatch has occurred-----,ERROR in 40
# 1 ERROR! See log above for details.
# quit
# End time: 20:07:50 on Dec 01,2023, Elapsed time: 0:00:01
# Errors: 0, Warnings: 0

3.3 TestBench Output

1
2
------ERROR. A mismatch has occurred-----,ERROR in          40
1 ERROR! See log above for details.

Model Technology ModelSim SE-64 vlog 10.7 Compiler 2017.12 Dec 7 2017
Start time: 18:30:35 on Nov 24,2023
vlog -work work ./design/multiply.v ./design/testbench.v -l vcompile.txt
– Compiling module multiply
– Compiling module tb

Top level modules:
tb
End time: 18:30:35 on Nov 24,2023, Elapsed time: 0:00:00
Errors: 0, Warnings: 1

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

0%