📄 mmregread.m
字号:
function resp = mmregread(cc,mmr,represent,timeout)
% Private.
% Reads the contents of a TMS320C54x memory-mapped register specified by MMR.
% Copyright 2002 The MathWorks, Inc.
% $Revision: 1.6 $ $Date: 2002/05/07 14:44:03 $
nargchk(4,4,nargin);
if ~isempty(strmatch(upper(mmr),{'A','B'},'exact'))
error(['Cannot read accumulator ''' upper(mmr) ''', ' ...
'read each component instead (' upper(mmr) 'L,' upper(mmr) 'H,' upper(mmr) 'G) ']);
end
% Do not map register-represention to memory-representation on
% special registers (non 16-bit regs) right away
if isempty(strmatch(upper(mmr),{'A','B','AG','BG'},'exact')) & ...
isempty(findstr(':',mmr))
represent = Get16BitRepresent(mmr,represent);
end
% Read contents of register(s)
switch upper(mmr)
case 'AR0'
% % addr = {'10' '1'};
addr = [16 1];
resp = read(cc,addr,represent,1,timeout);
case 'AR1'
% % addr = {'11' '1'};
addr = [17 1];
resp = read(cc,addr,represent,1,timeout);
case 'AR2'
% addr = {'12' '1'};
addr = [18 1];
resp = read(cc,addr,represent,1,timeout);
case 'AR3'
% addr = {'13' '1'};
addr = [19 1];
resp = read(cc,addr,represent,1,timeout);
case 'AR4'
% addr = {'14' '1'};
addr = [20 1];
resp = read(cc,addr,represent,1,timeout);
case 'AR5'
% addr = {'15' '1'};
addr = [21 1];
resp = read(cc,addr,represent,1,timeout);
case 'AR6'
% addr = {'16' '1'};
addr = [22 1];
resp = read(cc,addr,represent,1,timeout);
case 'AR7'
% addr = {'17' '1'};
addr = [23 1];
resp = read(cc,addr,represent,1,timeout);
case 'A',
resp = ReadAccumulator(cc,'A',[8,9,10],represent,timeout);
case 'AL'
% addr = {'8' '1'};
addr = [8 1];
resp = read(cc,addr,represent,1,timeout);
case 'AH'
% addr = {'9' '1'};
addr = [9 1];
resp = read(cc,addr,represent,1,timeout);
case 'AG'
% addr = {'A' '1'};
resp = Read8BitGuardReg(cc,'AG',10,represent,timeout);
case 'B'
resp = ReadAccumulator(cc,'B',[11,12,13],represent,timeout);
case 'BL'
% addr = {'B' '1'};
addr = [11 1];
resp = read(cc,addr,represent,1,timeout);
case 'BH'
% addr = {'C' '1'};
addr = [12 1];
resp = read(cc,addr,represent,1,timeout);
case 'BG'
% addr = {'D' '1'};
resp = Read8BitGuardReg(cc,'BG',13,represent,timeout);
case 'T'
% addr = {'E' '1'};
addr = [14 1];
resp = read(cc,addr,represent,1,timeout);
case 'TRN'
% addr = {'F' '1'};
addr = [15 1];
resp = read(cc,addr,represent,1,timeout);
case 'SP'
% addr = {'18' '1'};
addr = [24 1];
resp = read(cc,addr,represent,1,timeout);
case 'XPC'
% addr = {'1E' '1'};
addr = [30 1];
resp = read(cc,addr,'uint8',1,timeout);
case 'PC'
% no mapping in memory
resp = regread(cc,'PC',represent,timeout);
otherwise
if findstr(':',upper(mmr))
resp = ReadRegisterPair(cc,upper(mmr),represent,timeout);
else
error(['REGISTER ''' mmr ''' not recognized']);
end
end
resp = double(resp);
%---------------------------------
function resp = ReadRegisterPair(cc,mmr,represent,timeout)
colon = findstr(':',upper(mmr));
reglo = mmr(1:colon-1); % right reg
loval = mmregread(cc,reglo,'binary',timeout);
reghi = mmr(colon+1:end); % left reg
hival = mmregread(cc,reghi,'binary',timeout) * 2^16;
resp = loval + hival;
if strcmpi(represent,'binary')
elseif strcmpi(represent,'2scomp')
bin = dec2bin(resp,32) - '0';
resp = (-1*bin(1)*(2*(2^31))) + (bin(2:32) * (2 .^ [30:-1:0])');
elseif strcmpi(represent,'ieee')
bin = dec2bin(resp);
resp = bin2float(bin);
end
%-------------------------------
function resp = ReadAccumulator(cc,acc,address,represent,timeout)
% Read 40-bit accumulator (AL,AH,AG); Bigh Endian
AddrLow = address(1);
AddrHigh = address(2);
AddrGuard = address(3);
raw(1) = double( read(cc,[AddrLow 1],'uint16',1,timeout) ) * 2^16;
raw(2) = double( read(cc,[AddrHigh 1],'uint16',1,timeout) );
if strcmpi(represent,'binary')
raw(3) = double( read(cc,[AddrGuard 1],'uint8' ,1,timeout) ) * 2^32;
resp = sum(raw);
elseif strcmpi(represent,'2scomp')
raw(3) = double( read(cc,[AddrGuard 1],'uint8' ,1,timeout) ) * 2^32;
ndx = 40;
% if raw(3)>0
% guard_bin = dec2bin(raw(3),8)-'0';
% one_found = find(guard_bin==1);
% ndx = 32 + 8-one_found(1) + 1;
% raw(3) = raw(3) * 2^32;
% else
% ndx = 32;
% end
resp = sum(raw);
bin = dec2bin(resp,ndx) - '0';
resp = (-1*bin(1)*(2*(2^(ndx-1)))) + (bin(2:ndx) * (2 .^ [ndx-2:-1:0])');
elseif strcmpi(represent,'ieee')
warning(['IEEE format only uses ' acc 'L and ' acc 'H (single precision)']);
bin = dec2bin(raw(1)+raw(2));
resp = bin2float(bin);
end
%--------------------------------------
function represent = Get16BitRepresent(mmr,represent)
if strcmpi(represent,'binary')
represent= 'uint16';
elseif strcmpi(represent,'2scomp')
represent= 'int16';
elseif strcmpi(represent,'ieee')
error(['Contents of Register ''' upper(mmr) ''' cannot be represented in IEEE format']);
end
%--------------------------------------------
function resp = Read8BitGuardReg(cc,acc,address,represent,timeout);
if strcmpi(represent,'binary')
represent= 'uint8';
elseif strcmpi(represent,'2scomp')
represent= 'int8';
elseif strcmpi(represent,'ieee')
error(['Register ' acc ' cannot be represented in IEEE format']);
end
resp = read(cc,[address 1],represent,1,timeout);
%------------------------
function y = bin2float(bin)
wordlength = 32;
exponentlength = 8;
fractionlength = 23;
exponentbias = 127;
exponentmin = -126;
exponentmax = 127;
if isempty(bin)
y = 0;
return
end
% Zero pad to the right.
w = wordlength;
[mbin,nbin] = size(bin);
if nbin<w
% Zero-pad to the right.
o = '0';
% bin = [bin,o(ones(mbin,1),ones(w-nbin,1))];
bin = [o(ones(mbin,1),ones(w-nbin,1)), bin]; % my change - Zero-pad to the left.
end
% Peel off [sign, exponent, fraction]
% Then, each piece is small enough to be a positive integer
% Sign
s = (-1).^bin2dec(bin(:,1));
% Unbiased exponent
e = bin2dec(bin(:,2:exponentlength+1));
% Biased exponent
b = e - exponentbias;
% Fraction
f = pow2(bin2dec(bin(:,exponentlength+2:end)),-fractionlength);
% Initialize the output. Zero is the default, so
y = zeros(size(s));
% Denormal
n = e==0 & f~=0;
y(n) = s(n).*pow2(f(n),exponentmin);
% Normal
n = e~=0 & b<=exponentmax;
y(n) = s(n).*pow2(1+f(n),b(n));
% NaN
n = b==exponentmax+1 & f~=0;
y(n) = nan;
% +-inf
n = b==exponentmax+1 & f==0;
y(n) = s(n).*inf;
% [EOF] mmregread.m
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -