MIPS架構下的MCU,指令集包含R-Type、I-Type、J-Type三種,在數電課程設計時為了給MCU編寫指令集,需要將彙編語言轉化成機器代碼,這裡分享一下自己寫的Matlab 的 GUI。 主函數 C2M 函數rig_f 用來尋找名稱對應的寄存器地址 函數rig_n 用來將5位十進位數轉換成 ...
MIPS架構下的MCU,指令集包含R-Type、I-Type、J-Type三種,在數電課程設計時為了給MCU編寫指令集,需要將彙編語言轉化成機器代碼,這裡分享一下自己寫的Matlab 的 GUI。
主函數 C2M
1 function varargout = C2M(varargin) 2 val = zeros(1,1); 3 4 % C2M MATLAB code for C2M.fig 5 % C2M, by itself, creates a new C2M or raises the existing 6 % singleton*. 7 % 8 % H = C2M returns the handle to a new C2M or the handle to 9 % the existing singleton*. 10 % 11 % C2M('CALLBACK',hObject,eventData,handles,...) calls the local 12 % function named CALLBACK in C2M.M with the given input arguments. 13 % 14 % C2M('Property','Value',...) creates a new C2M or raises the 15 % existing singleton*. Starting from the left, property value pairs are 16 % applied to the GUI before C2M_OpeningFcn gets called. An 17 % unrecognized property name or invalid value makes property application 18 % stop. All inputs are passed to C2M_OpeningFcn via varargin. 19 % 20 % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 21 % instance to run (singleton)". 22 % 23 % See also: GUIDE, GUIDATA, GUIHANDLES 24 25 % Edit the above text to modify the response to help C2M 26 27 % Last Modified by GUIDE v2.5 08-Jun-2018 13:07:05 28 29 % Begin initialization code - DO NOT EDIT 30 gui_Singleton = 1; 31 gui_State = struct('gui_Name', mfilename, ... 32 'gui_Singleton', gui_Singleton, ... 33 'gui_OpeningFcn', @C2M_OpeningFcn, ... 34 'gui_OutputFcn', @C2M_OutputFcn, ... 35 'gui_LayoutFcn', [] , ... 36 'gui_Callback', []); 37 if nargin && ischar(varargin{1}) 38 gui_State.gui_Callback = str2func(varargin{1}); 39 end 40 41 if nargout 42 [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 43 else 44 gui_mainfcn(gui_State, varargin{:}); 45 end 46 % End initialization code - DO NOT EDIT 47 48 49 % --- Executes just before C2M is made visible. 50 function C2M_OpeningFcn(hObject, eventdata, handles, varargin) 51 % This function has no output args, see OutputFcn. 52 % hObject handle to figure 53 % eventdata reserved - to be defined in a future version of MATLAB 54 % handles structure with handles and user data (see GUIDATA) 55 % varargin command line arguments to C2M (see VARARGIN) 56 57 % Choose default command line output for C2M 58 handles.output = hObject; 59 60 % Update handles structure 61 guidata(hObject, handles); 62 63 % UIWAIT makes C2M wait for user response (see UIRESUME) 64 % uiwait(handles.figure1); 65 66 67 % --- Outputs from this function are returned to the command line. 68 function varargout = C2M_OutputFcn(hObject, eventdata, handles) 69 % varargout cell array for returning output args (see VARARGOUT); 70 % hObject handle to figure 71 % eventdata reserved - to be defined in a future version of MATLAB 72 % handles structure with handles and user data (see GUIDATA) 73 74 % Get default command line output from handles structure 75 varargout{1} = handles.output; 76 77 78 79 function edit1_Callback(hObject, eventdata, handles) 80 % hObject handle to edit1 (see GCBO) 81 % eventdata reserved - to be defined in a future version of MATLAB 82 % handles structure with handles and user data (see GUIDATA) 83 84 % Hints: get(hObject,'String') returns contents of edit1 as text 85 % str2double(get(hObject,'String')) returns contents of edit1 as a double 86 87 88 % --- Executes during object creation, after setting all properties. 89 function edit1_CreateFcn(hObject, eventdata, handles) 90 % hObject handle to edit1 (see GCBO) 91 % eventdata reserved - to be defined in a future version of MATLAB 92 % handles empty - handles not created until after all CreateFcns called 93 94 % Hint: edit controls usually have a white background on Windows. 95 % See ISPC and COMPUTER. 96 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 97 set(hObject,'BackgroundColor','white'); 98 end 99 100 101 102 function edit2_Callback(hObject, eventdata, handles) 103 % hObject handle to edit2 (see GCBO) 104 % eventdata reserved - to be defined in a future version of MATLAB 105 % handles structure with handles and user data (see GUIDATA) 106 107 % Hints: get(hObject,'String') returns contents of edit2 as text 108 % str2double(get(hObject,'String')) returns contents of edit2 as a double 109 110 111 % --- Executes during object creation, after setting all properties. 112 function edit2_CreateFcn(hObject, eventdata, handles) 113 % hObject handle to edit2 (see GCBO) 114 % eventdata reserved - to be defined in a future version of MATLAB 115 % handles empty - handles not created until after all CreateFcns called 116 117 % Hint: edit controls usually have a white background on Windows. 118 % See ISPC and COMPUTER. 119 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 120 set(hObject,'BackgroundColor','white'); 121 end 122 123 124 % --- Executes on button press in pushbutton1. 125 function pushbutton1_Callback(hObject, eventdata, handles) 126 global val 127 % hObject handle to pushbutton1 (see GCBO) 128 % eventdata reserved - to be defined in a future version of MATLAB 129 % handles structure with handles and user data (see GUIDATA) 130 131 str1 = get(handles.edit2, 'String'); %?????? 132 133 x = size(str1, 2); %???????? 134 y = size(str1, 1); %???????? 135 %b = zeros(y, x); %b?????????????????? 136 137 for i = 1 : y 138 139 opcode1 = str1(i,1); 140 opcode2 = strcat(str1(i,1), str1(i,2)); 141 opcode3 = strcat(str1(i,1), str1(i,2), str1(i,3)); 142 opcode4 = strcat(str1(i,1), str1(i,2), str1(i,3), str1(i,4)); 143 rd = strcat(str1(i, 6), str1(i, 7)); 144 rs = strcat(str1(i,11), str1(i,12)); 145 rt = strcat(str1(i,16), str1(i,17)); 146 147 if (x >= 19) 148 imm_sh = strcat(str1(i,15), str1(i,16), str1(i,17), str1(i, 18), str1(i, 19)); 149 end 150 rt4 = strcat(str1(i,7), str1(i,8)); 151 rs4 = strcat(str1(i,12), str1(i,13)); 152 if (x >= 20) imm4 = strcat(str1(i,16), str1(i,17), str1(i, 18), str1(i, 19), str1(i, 20)); 153 end 154 155 rd2 = strcat(str1(i, 5), str1(i, 6)); 156 rs2 = strcat(str1(i,10), str1(i,11)); 157 rt2 = strcat(str1(i,15), str1(i,16)); 158 rt_w = strcat(str1(i, 5), str1(i, 6)); 159 imm_w = strcat(str1(i, 9), str1(i, 10), str1(i, 11), str1(i, 12), str1(i, 13)); 160 rs_w = strcat(str1(i, 16), str1(i, 17)); 161 162 switch opcode4 163 case 'addi' 164 b(i,1)=0;b(i,2)=0;b(i,3)=1;b(i,4)=0;b(i,5)=0;b(i,6)=0; 165 b(i, 7:11) = rig_f(rs4); b(i, 12:16) = rig_f(rt4); 166 b(i, 17:32) = rig_n(imm4); 167 bq(i,:) = 'this is adi'; 168 %bqq(i,:) = strcat('this is addi ', rs4 , '-', rt4 , '-',imm4); 169 otherwise 170 switch opcode3 171 case 'add' 172 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 173 b(i,27)=1;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=0;b(i,32)=0; 174 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 175 bq(i,:) = 'this is add'; 176 %bqq(i,:) = strcat('this is add, ', rs , '-', rt , '-',rd); 177 case 'sub' 178 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 179 b(i,27)=1;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=1;b(i,32)=0; 180 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 181 bq(i,:) = 'this is sub';bqq(i,:) = strcat('this is sub, ', rs , '-', rt , '-',rd); 182 case 'and' 183 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 184 b(i,27)=1;b(i,28)=0;b(i,29)=0;b(i,30)=1;b(i,31)=0;b(i,32)=0; 185 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 186 bq(i,:) = 'this is and';bqq(i,:) = strcat('this is and, ', rs , '-', rt , '-',rd); 187 case 'slt' 188 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 189 b(i,27)=1;b(i,28)=0;b(i,29)=1;b(i,30)=0;b(i,31)=1;b(i,32)=0; 190 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 191 bq(i,:) = 'this is slt';bqq(i,:) = strcat('this is slt, ', rs , '-', rt , '-',rd); 192 case 'sll' 193 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 194 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=0;b(i,32)=0; 195 b(i, 7:11) = 0; b(i, 12:16) = rig_f(rs); b(i, 17:21) = rig_f(rd); b(i, 22:26) = rig_sh(imm_sh); 196 bq(i,:) = 'this is sll'; 197 % bqq(i,:) = strcat('this is sll, ', rs , '-', rd , '-',imm_sh); 198 case 'srl' 199 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 200 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=1;b(i,32)=0; 201 b(i, 7:11) = 0; b(i, 12:16) = rig_f(rs); b(i, 17:21) = rig_f(rd); b(i, 22:26) = rig_sh(imm_sh); 202 bq(i,:) = 'this is srl'; 203 % bqq(i,:) = strcat('this is srl, ', rs , '-', rd , '-',imm_sh); 204 case 'sra' 205 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 206 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=1;b(i,32)=1; 207 b(i, 7:11) = 0; b(i, 12:16) = rig_f(rs); b(i, 17:21) = rig_f(rd); b(i, 22:26) = rig_sh(imm_sh); 208 bq(i,:) = 'this is sra'; 209 case 'beq' 210 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=1;b(i,5)=0;b(i,6)=0; 211 b(i, 7:11) = rig_f(rd); b(i, 12:16) = rig_f(rs); b(i, 17:32) = rig_n(imm_sh); 212 bq(i,:) = 'this is beq'; 213 %bqq(i,:) = strcat('this is beq, ', rd , '-', rs , '-',imm_sh); 214 case 'bne' 215 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=1;b(i,5)=0;b(i,6)=1; 216 b(i, 7:11) = rig_f(rd); b(i, 12:16) = rig_f(rs); b(i, 17:32) = rig_n(imm_sh); 217 bq(i,:) = 'this is bne'; 218 %bqq(i,:) = strcat('this is bne, ', rd , '-', rs , '-',imm_sh); 219 case 'mul' 220 b(i,1)=0;b(i,2)=1;b(i,3)=1;b(i,4)=1;b(i,5)=0;b(i,6)=0; 221 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=0;b(i,31)=0;b(i,32)=1; 222 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 223 bq(i,:) = 'this is mul';bqq(i,:) = strcat('this is mul, ', rs , '-', rt , '-',rd); 224 case 'muf' 225 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 226 b(i,27)=0;b(i,28)=0;b(i,29)=0;b(i,30)=1;b(i,31)=0;b(i,32)=1; 227 b(i, 7:11) = rig_f(rs); b(i, 12:16) = rig_f(rt); b(i, 17:21) = rig_f(rd); b(i, 22:26) = 0; 228 bq(i,:) = 'this is mul';bqq(i,:) = strcat('this is mul, ', rs , '-', rt , '-',rd); 229 otherwise 230 switch opcode1 231 case 'j' 232 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=1;b(i,6)=0; 233 b(i, 7:32) = 9; 234 bq(i,:) = 'this is jrr';bqq(i,:) = strcat('this is j ', '66666666666666666666'); 235 otherwise 236 end 237 end 238 end 239 240 switch opcode2 241 case 'or' 242 b(i,1)=0;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=0;b(i,6)=0; 243 b(i,27)=1;b(i,28)=0;b(i,29)=0;b(i,30)=1;b(i,31)=0;b(i,32)=1; 244 b(i, 7:11) = rig_f(rs2); b(i, 12:16) = rig_f(rt2); b(i, 17:21) = rig_f(rd2); b(i, 22:26) = 0; 245 bq(i,:) = 'this is orr'; 246 247 case 'lw' 248 b(i,1)=1;b(i,2)=0;b(i,3)=0;b(i,4)=0;b(i,5)=1;b(i,6)=1; 249 b(i, 7:11) = rig_f(rs_w);b(i, 12:16) = rig_f(rt_w); b(i, 17:32) = rig_n(imm_w); 250 251 % bqq(i,:) = strcat('this is lwr, ', rs_w, '-', rt_w, '-',imm_w); 252 bq(i,:) = 'this is lwr'; 253 254 case 'sw' 255 b(i,1)=1;b(i,2)=0;b(i,3)=1;b(i,4)=0;b(i,5)=1;b(i,6)=1; 256 b(i, 7:11) = rig_f(rs_w);b(i, 12:16) = rig_f(rt_w); b(i, 17:32) = rig_n(imm_w); 257 bq(i,:) = 'this is swr'; 258 otherwise 259 end 260 261 262 end 263 264 bb = num2str(b); 265 set(handles.edit1, 'String', bb); 266 set(handles.edit3, 'String', bq); 267 val = b; 268 269 % --- Executes on button press in pushbutton2. 270 function pushbutton2_Callback(hObject, eventdata, handles) 271 global val 272 % hObject handle to pushbutton2 (see GCBO) 273 % eventdata reserved - to be defined in a future version of MATLAB 274 % handles structure with handles and user data (see GUIDATA) 275 dlmwrite('C:\Users\ALIENWARE\Desktop\fft_test.txt', val, 'delimiter', '', 'precision', 6, 'newline','pc'); 276 277 278 279 function edit3_Callback(hObject, eventdata, handles) 280 % hObject handle to edit3 (see GCBO) 281 % eventdata reserved - to be defined in a future version of MATLAB 282 % handles structure with handles and user data (see GUIDATA) 283 284 % Hints: get(hObject,'String') returns contents of edit3 as text 285 % str2double(get(hObject,'String')) returns contents of edit3 as a double 286 287 288 % --- Executes during object creation, after setting all properties. 289 function edit3_CreateFcn(hObject, eventdata, handles) 290 % hObject handle to edit3 (see GCBO) 291 % eventdata reserved - to be defined in a future version of MATLAB 292 % handles empty - handles not created until after all CreateFcns called 293 294 % Hint: edit controls usually have a white background on Windows. 295 %