硬件乘法器模块的四种操作类型(无符号乘法、有符号乘法、无符号乘加、有符号乘加)是由写入的第一个操作数的位置决定的。这个模块有两个操作数寄存器:OP1和OP2、三个结果寄存器RESLO, RESHI, 和SUMEXT。RESLO寄存器存储结果的低字(低16位);RESHI寄存器存储结果的高字(高16位);SUMEXT寄存器存储结果的有关信息。结果在3个时钟周期后即可完成;写入OP2后的下一条指令即可读取结果,有一种情况例外:用间接寻址方式访问结果。用间接寻址方式访问结果时,读取结果之前需要有一条NOP指令。
操作数OP1有四个地址(MPY:0130h MPYS:0132h MAC:0134h MACS:0136h),这四个寄存器用来选择乘法的操作模式。写入第一个操作数寄存器决定用哪种操作:无符号 用符号等,但是不启动相乘操作;写入第二个操作数寄存器启动相乘的操作。计算完成后结果存入寄存器RESLO,RESHI, 和SUMEXT。
操作数1的四个地址对应的操作:
OP1 Address Register Name Operation0130h MPY Unsigned multiply(无符号乘法)0132h MPYS Signed multiply(有符号乘法)0134h MAC Unsigned multiply accumulate(无符号乘加)0136h MACS Signed multiply accumulate(有符号乘加)四种操作模式下高位结果寄存器的内容如下:
Mode RESHI ContentsMPY Upper 16-bits of the resultMPYS The MSB is the sign of the result. The remaining bits are the upper 15-bits of the result. Two’s complement notation is usedfor the result.MAC Upper 16-bits of the resultMACS Upper 16-bits of the result. Two’s complement notation is used for the result.四种操作模式SUMEXT 寄存器的内容:
Mode SUMEXTMPY SUMEXT is always 0000hMPYS SUMEXT contains the extended sign of the result 00000h Result was positive or zero 0FFFFh Result was negativeMAC SUMEXT contains the carry of the result 0000h No carry for result 0001h Result has a carryMACS SUMEXT contains the extended sign of the result 00000h Result was positive or zero 0FFFFh Result was negative连续乘法运算时,如果操作数1不需改变就可以运算,则可以不需要重新写入和以保存内容相同的数;但OP2必须重新写入以启动乘法运算。
MACS Underflow and Overflow(MACS时的上溢和下溢):硬件乘法器不检测有符号乘加时运算结果的上溢出和下溢出。结果的正数范围:0到7FFF FFFFh;负数范围:0FFFF FFFFh到8000 0000h。下溢出是两个负数的和结果寄存器得到的是正数,上溢出是两个正数的和结果寄存器得到的是负数。SUMEXT寄存器存储有结果的符号,可以根据它判断是否溢出(0000h 负数和 则上溢 0FFFFh 正数和 则下溢)。使用时 程序必须合适的检测、处理MACS的溢出情况。
程序示例(用户指南上给出的汇编示例):
所有乘数模式的例子如下。所有的8x8模式使用的寄存器的绝对地址,因为汇编器将不允许B访问到字寄存器时使用标准定义的文件标签。
; 16x16 Unsigned MultiplyMOV #01234h,&MPY ; Load first operandMOV #05678h,&OP2 ; Load second operand; ... ; Process results; 8x8 Unsigned Multiply. Absolute addressing.MOV.B #012h,&0130h ; Load first operandMOV.B #034h,&0138h ; Load 2nd operand; ... ; Process results; 16x16 Signed MultiplyMOV #01234h,&MPYS ; Load first operandMOV #05678h,&OP2 ; Load 2nd operand; ... ; Process results; 8x8 Signed Multiply. Absolute addressing.MOV.B #012h,&0132h ; Load first operandSXT &MPYS ; Sign extend first operandMOV.B #034h,&0138h; Load 2nd operandSXT &OP2 ; Sign extend 2nd operand; (triggers 2nd multiplication); ... ; Process results; 16x16 Unsigned Multiply AccumulateMOV #01234h,&MAC ; Load first operandMOV #05678h,&OP2 ; Load 2nd operand; ... ; Process results; 8x8 Unsigned Multiply Accumulate. Absolute addressingMOV.B #012h,&0134h ; Load first operandMOV.B #034h,&0138h ; Load 2nd operand; ... ; Process results; 16x16 Signed Multiply AccumulateMOV #01234h,&MACS ; Load first operandMOV #05678h,&OP2 ; Load 2nd operand; ... ; Process results; 8x8 Signed Multiply Accumulate. Absolute addressingMOV.B #012h,&0136h ; Load first operandSXT &MACS ; Sign extend first operandMOV.B #034h,R5 ; Temp. location for 2nd operandSXT R5 ; Sign extend 2nd operandMOV R5,&OP2 ; Load 2nd operand; ... ; Process results上面的程序虽然和标准的汇编差异比较大,但是有一定汇编基础的人还是很容易就能够看懂。这里的程序给出了多种方式写入操作数寄存器。
间接寻址结果寄存器时,在写入OP2操作数启动乘法后,至少需要一个指令的延迟后才能访问结果寄存器RESLO等;直接寻址时可以写入OP2后,下一条指令即可读取结果。示例程序(汇编):
; Access multiplier results with indirect addressingMOV #RESLO,R5 ; RESLO address in R5 for indirectMOV &OPER1,&MPY ; Load 1st operandMOV &OPER2,&OP2 ; Load 2nd operandNOP ; Need one cycle 写入两个操作数 乘法运算开始后 需要一个NOPMOV @R5+,&xxx ; Move RESLOMOV @R5,&xxx ; Move RESHI如果在写入OP1和写入OP2之间产生了中断,中断响应后,源操作数的计算模式丢失;运算结果不确定。为了避免这种情况的发生,在写入操作数时禁止中断或在中断响应函数中不使用硬件乘法器。如:
; Disable interrupts before using the hardware multiplierDINT ; Disable interruptsNOP ; Required for DINTMOV #xxh,&MPY ; Load 1st operandMOV #xxh,&OP2 ; Load 2nd operandEINT ; Interrupts may be enable before; Process results硬件部分就说这么多了,有什么不大明白的可以参考用户指南。