玩命加载中 . . .

在动态分配内存时,操作系统必须对其进行管理。有两种方式跟踪内存使用情况:位图和空闲链表。

  1. 使用位图的存储管理

使用位图方法时,内存可能被划分成小到几个字或大到几千字节的分配单元。每个分配单元对应于位图中的一位,0表示空闲,1表示占用(或者相反)。一块内存区和其对应的位图如下图所示。

image-20220517221848784

分配单元的大小是一个重要的设计因素。分配单元越小,位图越大。然而即使只有4个字节大小的分配单元,32位(4字节)的内存也只需要位图中的1位;32n位的内存需要n位的位图,所以位图只占用了1/32的内存。若选择比较大的分配单元,则位图更小。但若进程的大小不是分配单元的整数倍,那么在最后一个分配单元中就会有一定数量的内存被浪费了。

读Excel

xlrd

安装

#安装1.2.0版本,高版本不支持xlsx格式,说是xlsx安全性问题故不支持xlsx格式了,但自己使用就无所谓了
pip install xlrd==1.2.0

问题:使用python3.9读取excel时报错AttributeError: ‘ElementTree’ object has no attribute ‘getiterator’

解决办法:在新版python3.9中,windows中使用的更新删除了getiterator方法,所以我们老版本的xlrd库调用getiterator方法时会报错。打开xlrd库路径下的xlrd.py文件,将所有的getiterator()替换成iter(),总共有2处

安装

pip install googletrans==4.0.0rc1

使用

from googletrans import Translator 

translator = Translator(service_urls=[ 
    'translate.google.cn' 
]) 

print(translator.translate('中国人'))   #Translated(src=zh-CN, dest=en, text=Chinese people, pronunciation=None, extra_data="{'confiden...")
print(translator.translate('中国人不骗中国人',dest='ja'))   #Translated(src=zh-CN, dest=ja, text=中国人は中国人に嘘をつかない, pronunciation=Chūgokujin wa chūgokujin ni uso o tsukanai, extra_data="{'confiden...")

使用.text就能获取翻译文本了

安装xpinyin

pip install xpinyin -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

使用

from xpinyin import Pinyin

p = Pinyin()
res = p.get_pinyin('测试')
print(res)  #ce-shi

#带声调
res = p.get_pinyin('测试', tone_marks='marks')
print(res)  #cè-shì

如果需要首字母大写,可自行对字符串处理。

  1. 问题:在字典循环使用pop()时,出现RuntimeError: dictionary changed size during iteration错误

答:for k,v in dict.items()过程中不能改变字典的size,在Python3.x和2.x可以使用list创建字典keys的备份:

for k in d.keys():
    v = d[k]  #get value
  1. 如何在Python中打印正号的数字+
#方法1
print("{0:+}".format(37))

#方法2
print("%+d" % (37,))

#方法3:从Python3.6开始可以使用
print(f"{37:+}")

传参

class adder(width: Int = 8) extends Module {
  val io = new Bundle {
    val a = in UInt(width bits)
    val b = in UInt(width bits)
    val sum = out UInt(width bits)
  }
  io.sum := io.a + io.b
}

class top extends Module {
  val adder0 = new adder(16)
  val adder1 = new adder(32)
}
module top (
);

  wire       [15:0]   adder0_io_a;
  wire       [15:0]   adder0_io_b;
  wire       [31:0]   adder1_io_a;
  wire       [31:0]   adder1_io_b;
  wire       [15:0]   adder0_io_sum;
  wire       [31:0]   adder1_io_sum;

  adder adder0 (
    .io_a      (adder0_io_a[15:0]    ), //i
    .io_b      (adder0_io_b[15:0]    ), //i
    .io_sum    (adder0_io_sum[15:0]  )  //o
  );
  adder_1 adder1 (
    .io_a      (adder1_io_a[31:0]    ), //i
    .io_b      (adder1_io_b[31:0]    ), //i
    .io_sum    (adder1_io_sum[31:0]  )  //o
  );

endmodule

module adder_1 (
  input      [31:0]   io_a,
  input      [31:0]   io_b,
  output     [31:0]   io_sum
);


  assign io_sum = (io_a + io_b);

endmodule

module adder (
  input      [15:0]   io_a,
  input      [15:0]   io_b,
  output     [15:0]   io_sum
);


  assign io_sum = (io_a + io_b);

endmodule

二位数组

class top extends Module {
    val para_buf = Vec(UInt(8 bits),4)
    para_buf(0) := 1
    para_buf(1) := 2
    para_buf(2) := 3
    para_buf(3) := 4    
}
module top (
);

  wire       [7:0]    para_buf_0;
  wire       [7:0]    para_buf_1;
  wire       [7:0]    para_buf_2;
  wire       [7:0]    para_buf_3;

  assign para_buf_0 = 8'h01;
  assign para_buf_1 = 8'h02;
  assign para_buf_2 = 8'h03;
  assign para_buf_3 = 8'h04;

endmodule

外联RTL

class adder(width: Int) extends BlackBox {
  addGeneric("WIDTH", width)

  val io = new Bundle {
    val ain = in UInt(width bits)
    val bin = in UInt(width bits)
    val add_out = out UInt(width bits)
  }
  noIoPrefix()

  addRTLPath("rtl/adder.v")
}

class top extends Module {
  val io = new Bundle {
    val ain = in UInt(16 bits)
    val bin = in UInt(16 bits)
    val add_out = out UInt(16 bits)
  }
  noIoPrefix()
  var adder0 = new adder(16)
  io.ain <> adder0.io.ain
  io.bin <> adder0.io.bin
  io.add_out <> adder0.io.add_out
}
module top (
  input      [15:0]   ain,
  input      [15:0]   bin,
  output     [15:0]   add_out
);

  wire       [15:0]   adder0_add_out;

  adder #(
    .WIDTH(16) 
  ) adder0 (
    .ain        (ain[15:0]             ), //i
    .bin        (bin[15:0]             ), //i
    .add_out    (adder0_add_out[15:0]  )  //o
  );
  assign add_out = adder0_add_out;

endmodule

自定义时钟域

class top extends Module{
    val io = new Bundle {
    val clk_a = in Bool()
    val rst_a_n = in Bool()
    val clk_b = in Bool()
    val rst_b_n = in Bool()
    }
    noIoPrefix()

    //1、创建clockDomain
    val cd_a = ClockDomain(io.clk_a, io.rst_a_n, config = ClockDomainConfig(resetActiveLevel = LOW))
    val cd_b = ClockDomain(io.clk_b, io.rst_b_n, config = ClockDomainConfig(resetActiveLevel = LOW))

    //2、clockDomain a logic
    val cnt_a = cd_a on UInt(8 bits) setAsReg() init 0
    val wr_en_a = cd_a on Bool()
    when(wr_en_a) {
        cnt_a := cnt_a + 1
    }
    val cnt_a_gray = toGray(cnt_a)

    //3、clockDomain b logic
    val cnt_b = cd_b on UInt(8 bits) setAsReg() init 0
    val wr_en_b = cd_b on Bool()
    when(wr_en_b) {
        cnt_b := cnt_b + 1
    }

    //4、clockDomain cross logic
    val cnt_a2b = cd_b on RegNext(cnt_a_gray) init 0 addTag(crossClockDomain)
    val cnt_a2b_sync1 = cd_b on RegNext(cnt_a2b) init 0
    val cnt_a2b_sync2 = cd_b on RegNext(cnt_a2b_sync1) init 0
}
module top (
  input               clk_a,
  input               rst_a_n,
  input               clk_b,
  input               rst_b_n
);

  wire       [7:0]    cnt_a_gray_bits;
  reg        [7:0]    cnt_a;
  wire                wr_en_a;
  wire       [7:0]    cnt_a_gray;
  reg        [7:0]    cnt_b;
  wire                wr_en_b;
  (* async_reg = "true" *) reg        [7:0]    cnt_a2b;
  reg        [7:0]    cnt_a2b_sync1;
  reg        [7:0]    cnt_a2b_sync2;

  assign cnt_a_gray_bits = (cnt_a >>> 1'b1);
  assign cnt_a_gray = (cnt_a_gray_bits ^ cnt_a);
  always @(posedge clk_a or negedge rst_a_n) begin
    if(!rst_a_n) begin
      cnt_a <= 8'h0;
    end else begin
      if(wr_en_a) begin
        cnt_a <= (cnt_a + 8'h01);
      end
    end
  end

  always @(posedge clk_b or negedge rst_b_n) begin
    if(!rst_b_n) begin
      cnt_b <= 8'h0;
      cnt_a2b <= 8'h0;
      cnt_a2b_sync1 <= 8'h0;
      cnt_a2b_sync2 <= 8'h0;
    end else begin
      if(wr_en_b) begin
        cnt_b <= (cnt_b + 8'h01);
      end
      cnt_a2b <= cnt_a_gray;
      cnt_a2b_sync1 <= cnt_a2b;
      cnt_a2b_sync2 <= cnt_a2b_sync1;
    end
  end


endmodule

Enum

class top extends Module{
    val io = new Bundle {
        val start = in Bool()
    }
    
    object mainState extends SpinalEnum {
        val IDLE,TX_DATA,DONE = newElement()
        rawElementName()  //状态机名去掉object名字前缀
    }
    val cstate = mainState() setAsReg() init(mainState.IDLE)
    val nstate = mainState()
    
    val cnt = Reg(UInt(5 bits)) init(0)
    when(io.start) {
        cnt := 0
    } elsewhen(cstate === mainState.TX_DATA) {
        cnt := cnt + 1
    }
    
    cstate := nstate
    switch(cstate) {
        is(mainState.IDLE) {
            when(io.start) {
                nstate := mainState.TX_DATA
            } otherwise {
                nstate := cstate
            }
        }
        is(mainState.TX_DATA) {
            when(cnt === U"5'd31") {
                nstate := mainState.DONE
            } otherwise {
                nstate := cstate
            }
        }
        is(mainState.DONE) {
            nstate := mainState.IDLE
        }
    }
        
}
module top (
  input               io_start,
  input               clk,
  input               rstn
);
  localparam IDLE = 2'd0;
  localparam TX_DATA = 2'd1;
  localparam DONE = 2'd2;

  reg        [1:0]    cstate;
  reg        [1:0]    nstate;
  reg        [4:0]    cnt;

  always @(*) begin
    case(cstate)
      IDLE : begin
        if(io_start) begin
          nstate = TX_DATA;
        end else begin
          nstate = cstate;
        end
      end
      TX_DATA : begin
        if((cnt == 5'h1f)) begin
          nstate = DONE;
        end else begin
          nstate = cstate;
        end
      end
      default : begin
        nstate = IDLE;
      end
    endcase
  end

  always @(posedge clk or negedge rstn) begin
    if(!rstn) begin
      cstate <= IDLE;
      cnt <= 5'h0;
    end else begin
      if(io_start) begin
        cnt <= 5'h0;
      end else begin
        if((cstate == TX_DATA)) begin
          cnt <= (cnt + 5'h01);
        end
      end
      cstate <= nstate;
    end
  end


endmodule