Step 7 of Self-writing the CPU (6) -- multiplication and accumulation command implementation

Source: Internet
Author: User

I will upload my new book "Write CPU by myself". Today is 29th articles. I try to write them every Thursday.


The sales address of Amazon is as follows. You are welcome to look around!

Http://www.amazon.cn/dp/b00mqkrlg8/ref=cm_sw_r_si_dp_5kq8tb1gyhja4


The sales address of China-pub is as follows (including the catalog, content introduction, and preface ):

Http://product.china-pub.com/3804025


7.6 instructions on Multiplication, accumulation, and Subtraction

There are four commands, namely Madd, maddu, Msub, and msubu, in the format of 7-11. We can see that the instruction code of these four commands is special, and the 6-15bit command is 0. You can determine which instruction is based on the Function Code of the 0-5bit command.


  • When the function code is 6'b000000, it indicates that it is a MADD command and a symbolic multiplication and accumulation operation.

Command usage: Madd RS, RT

Command: {hi, lo} <-{hi, lo} + Rs × rt. Set the address to the value of the General Register of Rs, the value of the general register whose address is Rt is used as the number of symbols for multiplication. The calculation result is added to {hi, lo} and the addition result is saved to {hi, lo. Here {hi, lo} indicates the 64-bit number formed by the connection between hi and lo registers. Hi indicates the high 32-bit and lo indicates the low 32-bit.

  • When the function code is 6'b000001, it indicates that it is a maddu command, and the Ampersand is an ampersand.

Command usage: maddu RS, RT

Command: {hi, lo} <-{hi, lo} + Rs × rt. Set the address to the value of the General Register of Rs, the value of the general register whose address is Rt is used as the unsigned number for multiplication. The calculation result is added to {hi, lo}, and the addition result is saved to {hi, lo.

  • When the function code is 6'b000100, it indicates that it is an Msub command, which is a symbolic multiplication and subtraction operation.

Command usage: Msub RS, RT

Command: {hi, lo} <-{hi, lo}-Rs × rt. Set the address to the value of the General Register of Rs, the value of the General Register with the address RT, which is used as the number of symbols for multiplication. Then, use {hi, lo} to subtract the multiplication result and save the subtraction result to {hi, lo.

  • When the function code is 6'b000101, it indicates that it is an msubu command and the Ampersand multiplication and subtraction operation.

Command usage: msubu RS, RT

Command: {hi, lo} <-{hi, lo}-Rs × rt. Set the address to the value of the General Register of Rs, the value of the general register whose address is Rt, which is used as the unsigned number for multiplication. Then, use {hi, lo} to subtract the multiplication result and save the subtraction result to {hi, lo.

7.7 how to implement the multiplication, accumulation, and subtraction commands 7.7.1

At the beginning of this chapter, we have explained how to implement the multiplication, accumulation, and multiplication commands. We plan to use two clock cycles to complete the operation in the pipeline stage, and perform the multiplication operation in the first clock cycle, in the second cycle, the multiplication result is added/subtracted from the HI and lo registers.

7.7.2 System Structure Modification

To implement the idea in section 7.7.1, two information must be saved: (1) the current clock cycle and (2) the multiplication result. Openmips saves the above information by adding two registers CNT and HiLo to the ex/MEM module. Modify the system structure as shown in Figure 7-12.


In the execution phase, the output hilo_temp_o of the ex module is the multiplication result, which is passed to the EX/MEM module and sent back to the ex module in the next clock cycle for the addition/subtraction operation of the second clock cycle.

In the execution phase, the output cnt_o of the ex module indicates the number of current clock cycles, which are passed to the EX/MEM module and sent back to the ex module in the next clock cycle, the latter determines the number of execution cycles currently in the multiplication, accumulation, and subtraction commands.


The code for implementing the multiplication, accumulation, and subtraction commands will be provided next time. Stay tuned!

Step 7 of Self-writing the CPU (6) -- multiplication and accumulation command implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.