source: PlatformSupport/sysgen2opb/sysgen2opb.m

Last change on this file was 835, checked in by murphpo, 16 years ago

updating copyright date

File size: 76.1 KB
Line 
1% Copyright (c) 2006-2008 Rice University
2% All Rights Reserved
3% This code is covered by the Rice-WARP license
4% See http://warp.rice.edu/license/ for details
5%
6% 'sysgen2opb' is a MATLAB script which converts a model built in Xilinx
7% System Generator into an OPB-compliant peripheral for use with the
8% embedded PowerPCs. The script replaces all the model's From/To Registerse
9% with memory mapped registers. It also creates the necessary address
10% decode logic and a C header file with the resulting register map.
11%
12% In addition, there is an option that can be turned on/off:
13% Shared Memory Extension Script - This script adds in an option to allow
14% the usage of Shared Memory. This script requires the usage of Dual Port
15% RAM's.
16%
17% This script is only supported for Xilinx System Generator 8.1 and 8.2
18%
19% To run the script, type the following on the Matlab command line:
20% sysgen2opb('yourModelName', 'yourSMChoice')
21%
22%   Replace 'yourModelName' with your Simulink model's name
23%   without the .mdl extension.
24%
25%   Replace 'yourSMChoice' with one of the following:
26%       1. 'smon' which will turn on the Shared Memory Extension Script
27%       2. 'smoff' which will turn off the Shared Memory Extension Script
28%
29% If you want to run the original script that utilizes From/To Registers
30% with no shared memory extension, run: sysgen2opb('yourModelName')
31
32function sysgen2opb(sys, varargin)
33warning off;
34
35% Run the model to see if there are any errors in the model
36try
37    sim(sys, 0)
38catch
39    display(['Error: Simulating the model resulted in error. Please check your model and initialization script.']);
40    return;
41end 
42
43% Base Address is always zero due to relocatable memory
44base = 0;
45
46appendString = '';
47
48% Look at the length of varargin to determine whether to run default
49% scripts
50if length(varargin) == 0
51    %Defult to no shared memory
52    smChoice = 'smoff';
53elseif length(varargin) == 1
54    smChoice = char(varargin(1));
55else
56    display('Unrecognized parameters on command line.');
57    return;
58end
59
60% Find the sample time period from the System Generator block
61sysgenblock = find_system(sys, 'block_type', 'sysgen');
62if length(sysgenblock) == 0
63    display(['Error: Please include a System Generator block.']);
64    return;
65elseif length(sysgenblock) > 1
66    display(['Error: Please only include one System Generator block in model.']);
67    return;
68else
69    sysgenperiod = get_param(sysgenblock, 'simulink_period');
70    sysgenperiod = mat2str(cell2mat(sysgenperiod));
71end
72
73% Load the model. Inform the user if it fails to load (ie: the model does
74% not exist)
75try
76    load_system(sys);
77catch
78    display(['Error: System ' sys ' does not exist']);
79    return;
80end
81
82% Check smChoice
83if ~(strcmp(smChoice, 'smon') | strcmp(smChoice, 'smoff'))
84    display(['Error smChoice input ''' smChoice ''' does not exist. Please use ''smon'' or ''smoff'' as your inputs.']);
85    return;
86end
87
88
89% Find the current sysgen version
90try
91    version = xlVersion;
92catch
93    display('Error: Sysgen is not found!');
94    return;
95end
96
97if length(version) >= 1
98    % The latest version is the first one in the cell
99    version = version{1};
100    % Get the first digit of the string array
101    version = str2num(version(1:3)); 
102else
103    display('Error: Sysgen is not found!');
104    return;
105end
106
107if ((version == 8.1) | (version == 8.2) | (version == 9.1))
108    % Find the From/To Registers
109    gatewaysInCheck = find_system(sys, 'LookUnderMasks', 'all', 'FollowLinks', 'on',   'block_type', 'fromreg');
110    gatewaysOutCheck = find_system(sys, 'LookUnderMasks', 'all', 'FollowLinks', 'on',    'block_type', 'toreg');
111else
112    display(['Error: Version ' version ' is not supported.']);
113    return;
114end
115
116% Check how many Gateways we have
117numberGateways = length(gatewaysInCheck) + length(gatewaysOutCheck);
118if numberGateways > 450
119    display('Error: The script only allows a max of 450 From/To Registers');
120    return;
121elseif numberGateways == 0
122    display('Error: The script needs From/To Regs within the model');
123    return;
124end
125
126% Check to see if we have a converted model by checking if we have the
127% Memory Mapped IO
128% If we do, inform the user and exit the script
129MemoryMappedIO = find_system(sys, 'LookUnderMasks', 'all', 'FollowLinks', 'on',  'BlockType', 'SubSystem', 'Name', 'Memory Mapped I_O');
130if length(MemoryMappedIO) > 0
131    display('You already have converted this design to a peripheral.');
132    display('Please get rid of the Memory Mapped I_O block and make sure')
133    display('your gateways are all still in place before trying again.');
134    return;
135end
136
137% Try saving an unconverted backup
138try
139    save_system(sys,[sys '_unconverted_backup']);
140catch
141    % There's a system with that name that's opened right now
142    % Close that system without saving
143    close_system([sys '_unconverted_backup'], 0);
144
145    % Save the backup system
146    save_system(sys,[sys '_unconverted_backup']);
147end
148close_system([sys '_unconverted_backup']);
149
150% Load the system
151load_system(sys);
152save_system(sys, sys, 'BreakLinks');
153
154% Try loading a local copy from the skeleton model
155% If it is not there, load it from the toolbox
156spm = 'skeleton_periph_model';
157try
158    load_system(spm);
159catch
160    load_system([matlabroot '\toolbox\WARP\' spm]);
161end
162
163%Find Sybsystems and correct all the names
164subsystems = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on', 'BlockType', 'SubSystem');
165%First we need to look for duplicate names
166for c=1:length(subsystems)
167    subsysName = subsystems{c};
168    shortName = get_param(subsysName, 'Name');
169    parent = get_param(subsysName, 'Parent');
170    compName = [shortName '                 '];
171    % Make sure that we do not edit the name of the System Generator
172    if ~strcmp(compName(1:17), ' System Generator')
173        idx=0;  %appended to the end of a duplicate name
174        for d=c+1:length(subsystems)
175            subsysName2 = subsystems{d};
176            shortName2 = get_param(subsysName2,'Name');
177            parent2 = get_param(subsysName2, 'Parent');
178            if strcmp(parent, parent2) & strcmp(cleanName(shortName), cleanName(shortName2))
179                set_param(subsysName2,'Name', [shortName2 '_' int2str(idx)]);
180                idx=idx+1;           
181                subsystems = find_system(sys,'LookUnderMasks','all','BlockType','SubSystem')';
182            end
183        end
184    end
185end
186
187% Update the Subsystem list
188subsystems = find_system(sys, 'LookUnderMasks', 'all', 'FollowLinks', 'on',  'BlockType', 'SubSystem');
189%Now for Subsystems with spaces, we want to clean up the name
190for c=length(subsystems):-1:1
191    subsysName = subsystems{c};
192    shortName = get_param(subsysName, 'Name');
193    compName = [shortName '                 '];
194    % Make sure that we do not edit the name of the System Generator
195    if ~strcmp(compName(1:17), ' System Generator')
196        set_param(subsysName, 'Name', cleanName(shortName));
197    end
198end
199
200if strcmp(smChoice, 'smon')
201    % Define the variables that will be
202    % later used for shared memory extension
203    dpramWOAddr={};
204    dpramWOAddrIndex = 0;
205    dpramWODataI={};
206    dpramWODataIIndex = 0;
207    dpramWOWE={};
208    dpramWOWEIndex = 0;
209    dpramROAddr={};
210    dpramROAddrIndex = 0;
211    dpramRODataO={};
212    dpramRODataOIndex = 0;
213   
214    % dpramROIndex will be used to determine the number of dpram Read-only's
215    % This will be used to determine whether we need a delay of one within
216    % the mem_if_out
217    dpramROIndex = 0;
218   
219    % maxdpram will be used to determine how many shared memory blocks we
220    % can have
221    binBaseAddress = dec2bin(base);
222    try
223        maxdpram = 15 - bin2dec(binBaseAddress(13:16));
224    catch
225        maxdpram = 15;
226    end
227   
228    % dpram will hold the list of all dual port RAM's
229    dpram = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'block_type', 'dpram');
230
231    % dpramWORO will hold the list of all WO, RO dual port RAM's
232    dpramWORO={};
233    dpramWOROidx = 0;
234    for c=1:length(dpram)
235        dpramName = dpram{c};
236        compName = [get_param(dpramName, 'Name') '     '];
237
238        % Delete all broken lines
239        brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
240        delete_line(brokenlines);
241        brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'DstBlockHandle', -1);
242        delete_line(brokenlines);
243       
244        if strcmp(compName(1:5), 'SMWO_') | strcmp(compName(1:5), 'SMRO_')
245            dpramWOROidx = dpramWOROidx + 1;
246            if dpramWOROidx > maxdpram
247                display(['Error: We are allowed a total of ' num2str(maxdpram) ' dual port RAMS that are read-only/write-only.']);
248                return;
249            end
250           
251            dpramWORO{dpramWOROidx} = [get_param(dpramName, 'Name') '_sm' num2str(dpramWOROidx - 1)] ;
252           
253            DpramPortHandles = get_param(dpramName, 'PortHandles');
254
255            ports = [DpramPortHandles.Inport(4), DpramPortHandles.Inport(5), DpramPortHandles.Inport(6), DpramPortHandles.Outport(2)];
256
257            SrcBlockHandleAddr = get_param(get_param(ports(1), 'Line'), 'SrcBlockHandle');
258            SrcBlockHandleDataI = get_param(get_param(ports(2), 'Line'), 'SrcBlockHandle');
259            SrcBlockHandleWE = get_param(get_param(ports(3), 'Line'), 'SrcBlockHandle');
260           
261
262            AddrName = get_param(SrcBlockHandleAddr, 'Name');
263            CompAddrName = [AddrName '         '];
264            AddrFullName = [get_param(SrcBlockHandleAddr, 'Parent') '/' AddrName];
265            if ~strcmp(get_param(SrcBlockHandleAddr, 'block_type'), 'gatewayin')
266                display(['A GatewayIn is needed in order to connect to the addrb inport of the dpram ' dpramName '.']);
267                return;
268            end
269            if  ~strcmp(get_param(SrcBlockHandleAddr, 'arith_type'), 'Unsigned') | (str2num(get_param(SrcBlockHandleAddr, 'n_bits')) > 14) |  (str2num(get_param(SrcBlockHandleAddr, 'bin_pt')) > 0)
270                display(['The GatewayIn ' AddrFullName ' needs to be UFix, with number of bits <= 14, and binary point at 0.']);
271                return;
272            end
273            orientationAddr = get_param(SrcBlockHandleAddr, 'Orientation');
274            placeAddr = get_param(SrcBlockHandleAddr, 'position');
275            parentAddr = get_param(SrcBlockHandleAddr, 'Parent');
276            fromAddr = [parentAddr '/' cleanName(AddrName) '_sm' int2str(dpramWOROidx)];
277            fromAddrSlice = [parentAddr '/' cleanName(AddrName) '_slice_sm' int2str(dpramWOROidx)];
278            fromAddrSliceBits = get_param(SrcBlockHandleAddr, 'n_bits');
279           
280            DataIName = get_param(SrcBlockHandleDataI, 'Name');
281            CompDataIName = [DataIName '          '];
282            DataIFullName = [get_param(SrcBlockHandleDataI, 'Parent') '/' DataIName];
283            orientationDataI = get_param(SrcBlockHandleDataI, 'Orientation');
284            placeDataI = get_param(SrcBlockHandleDataI, 'position');
285            parentDataI = get_param(SrcBlockHandleDataI, 'Parent');
286            fromDataI = [parentAddr '/' cleanName(DataIName) '_sm' int2str(dpramWOROidx)];
287           
288            WEName = get_param(SrcBlockHandleWE, 'Name');
289            CompWEName = [WEName '       '];
290            WEFullName = [get_param(SrcBlockHandleWE, 'Parent') '/' WEName];
291            orientationWE = get_param(SrcBlockHandleWE, 'Orientation');
292            placeWE = get_param(SrcBlockHandleWE, 'position');
293            parentWE = get_param(SrcBlockHandleWE, 'Parent');
294            fromWE = [parentWE '/' cleanName(WEName) '_sm' int2str(dpramWOROidx)];
295           
296        end
297
298        if strcmp(compName(1:5), 'SMWO_')
299            if ~strcmp(CompAddrName(1:9), 'SMWOAddr_')
300                display(['The Gateway In ' AddrName ' needs to have the prefix of SMWOAddr_']);
301                return;
302            end
303           
304            %%%%% SMWOAddr %%%%%%
305            % Delete the GatewayIn
306            delete_block(SrcBlockHandleAddr);
307
308           
309            deleteSource(sys, cleanName(AddrName));
310           
311            % Find the broken lines that starts from the output port of the
312            % deleted GatewayIn
313            brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
314
315            % Find out the handle of the port and the block
316            % These two handles will be used to extract more information
317            DstPortHandle = get_param(brokenlines, 'DstPortHandle');
318            DstBlockHandle = get_param(brokenlines, 'DstBlockHandle');
319
320            % From DstPortHandle, we can find out what is
321            % the destination port number
322            if length(DstPortHandle) > 1
323                DstPortNumber = cell2mat(get_param(DstPortHandle{length(DstPortHandle)}, 'PortNumber'));
324            else
325                DstPortNumber = get_param(DstPortHandle, 'PortNumber');
326            end
327
328            % From DstBlockHandle, we can find out what is the name of the
329            % destination block
330            if length(DstBlockHandle) > 1
331                DstName = get_param(DstBlockHandle{length(DstBlockHandle)}, 'Name');
332                try
333                    DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'block_type');
334                catch
335                    DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'BlockType');
336                end
337            else
338                DstName = get_param(DstBlockHandle, 'Name');
339                try
340                    DstBlockType = get_param(DstBlockHandle, 'block_type');
341                catch
342                    DstBlockType = get_param(DstBlockHandle, 'BlockType');
343                end
344            end
345
346            % Delete the borken lines
347            delete_line(brokenlines);
348
349            dpramWOAddrIndex = dpramWOAddrIndex + 1;
350            dpramWOAddr{dpramWOAddrIndex} = fromAddr;
351            %placeAddr(1,1) = placeAddr(1,1) - 60;
352            placeAddr(1,3) = placeAddr(1,1) + 75;
353            placeAddrSlice = placeAddr;
354            placeAddrSlice(1,1) = placeAddrSlice(1,3) + 30;
355            placeAddrSlice(1,3) = placeAddrSlice(1,1) + 50;
356            add_block('built-in/From', fromAddr, 'GotoTag', ['SMWOAddr_' int2str(dpramWOAddrIndex - 1)], 'position', placeAddr);
357            if ((version == 8.1) | (version == 8.2) | (version == 9.1))
358                add_block([spm '/Slice8'], fromAddrSlice, 'position', placeAddrSlice, 'nbits', fromAddrSliceBits);
359            end
360                                   
361            FromName = get_param(fromAddr, 'Name');
362            FromSliceName = get_param(fromAddrSlice, 'Name');
363            set_param(fromAddr,'Orientation', orientationAddr);
364
365            add_line(parentAddr, [FromName '/1' ], [FromSliceName '/1']);
366            if size(DstName, 1) == 1
367               
368                add_line(parentAddr, [FromSliceName '/1'], [DstName '/' int2str(DstPortNumber)]);
369                %add_line(parentAddr, [FromName '/1' ], [DstName '/' int2str(DstPortNumber)]);
370            else
371                for i=1:size(DstName, 1)
372                    if ~strcmp(DstName{i}, get_param(dpramName, 'Name'))
373                        add_line(parentAddr, [FromName '/1'], [DstName{i} '/' mat2str(DstPortNumber(i))]);
374                    else
375                        add_line(parentAddr, [FromSliceName '/1'], [DstName{i} '/' mat2str(DstPortNumber(i))]);
376                    end
377                end
378            end
379           
380            %%%%% SMWODataI %%%%%%
381            if ~strcmp(get_param(SrcBlockHandleDataI, 'block_type'), 'gatewayin')
382                display(['A Gateway In is needed in order to connect to the datb inport of the dpram ' dpramName '.']);
383                return;
384            end
385            if ~strcmp(get_param(SrcBlockHandleDataI, 'n_bits'), '32')
386                display(['The Xilinx block ' DataIFullName ' needs to set the number of bits to 32.']);
387                return;
388            end
389            if ~strcmp(CompDataIName(1:10), 'SMWODataI_')
390                display(['The Xilinx block ' DataIName ' needs to have a prefix of SMWODataI_']);
391                return;
392            end
393            SMWODataIArithType = get_param(SrcBlockHandleDataI, 'arith_type');
394            SMWODataIArithType = [SMWODataIArithType '      '];
395            SMWODataIArithType = SMWODataIArithType(1:8);
396            if strcmp(SMWODataIArithType, 'Unsigned')
397               fromDataIForce = [parentAddr '/' cleanName(DataIName) '_force_sm' int2str(dpramWOROidx)];
398            end
399           
400            % Delete the GatewayIn
401            delete_block(SrcBlockHandleDataI);
402
403            deleteSource(sys, cleanName(DataIName));
404           
405            % Find the broken lines that starts from the output port of the
406            % deleted GatewayIn
407            brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
408
409            % Find out the handle of the port and the block
410            % These two handles will be used to extract more information
411            DstPortHandle = get_param(brokenlines, 'DstPortHandle');
412            DstBlockHandle = get_param(brokenlines, 'DstBlockHandle');
413
414            % From DstPortHandle, we can find out what is
415            % the destination port number
416            if length(DstPortHandle) > 1
417                DstPortNumber = cell2mat(get_param(DstPortHandle{length(DstPortHandle)}, 'PortNumber'));
418            else
419                DstPortNumber = get_param(DstPortHandle, 'PortNumber');
420            end
421
422            % From DstBlockHandle, we can find otu what is the name of the
423            % destination block
424            if length(DstBlockHandle) > 1
425                DstName = get_param(DstBlockHandle{length(DstBlockHandle)}, 'Name');
426                try
427                    DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'block_type');
428                catch
429                    DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'BlockType');
430                end
431            else
432                DstName = get_param(DstBlockHandle, 'Name');
433                try
434                    DstBlockType = get_param(DstBlockHandle, 'block_type');
435                catch
436                    DstBlockType = get_param(DstBlockHandle, 'BlockType');
437                end
438            end
439
440            % Delete the borken lines
441            delete_line(brokenlines);
442           
443            dpramWODataIIndex = dpramWODataIIndex + 1;
444            dpramWODataI{dpramWODataIIndex} = fromDataI;
445           
446            if strcmp(SMWODataIArithType, 'Unsigned')
447                %placeDataI(1,1) = placeDataI(1,1) + 75;
448                placeDataIForce = placeDataI;
449                placeDataIForce(1,1) = placeDataIForce(1,3) + 30;
450                placeDataIForce(1,3) = placeDataIForce(1,1) + 50;
451                add_block('built-in/From', fromDataI, 'GotoTag', ['SMWODataI_' int2str(dpramWODataIIndex - 1)], 'position', placeDataI);
452                add_block([spm '/Force8'], fromDataIForce, 'position', placeDataIForce); 
453               
454                FromName = get_param(fromDataI, 'Name');
455                FromForceName = get_param(fromDataIForce, 'Name');
456                set_param(fromDataI, 'Orientation', orientationDataI);
457               
458                add_line(parentAddr, [FromName '/1' ], [FromForceName '/1']);
459                if size(DstName, 1) == 1
460
461                    add_line(parentAddr, [FromForceName '/1'], [DstName '/' int2str(DstPortNumber)]);
462                    %add_line(parentAddr, [FromName '/1' ], [DstName '/' int2str(DstPortNumber)]);
463                else
464                    for i=1:size(DstName, 1)
465                        if ~strcmp(DstName{i}, get_param(dpramName, 'Name'))
466                            add_line(parentAddr, [FromName '/1'], [DstName{i} '/' mat2str(DstPortNumber(i))]);
467                        else
468                            add_line(parentAddr, [FromForceName '/1'], [DstName{i} '/' mat2str(DstPortNumber(i))]);
469                        end
470                    end
471                end
472            else           
473                placeDataI(1,1) = placeDataI(1,1) - 60;
474                add_block('built-in/From', fromDataI, 'GotoTag', ['SMWODataI_' int2str(dpramWODataIIndex - 1)], 'position', placeDataI);
475
476                FromName = get_param(fromDataI, 'Name');
477                set_param(fromDataI,'Orientation', orientationDataI);
478
479                if size(DstName, 1) == 1
480                    add_line(parentDataI, [FromName '/1' ], [DstName '/' int2str(DstPortNumber)]);
481                else
482                    for i=1:size(DstName, 1)
483                        add_line(parentDataI, [FromName '/1'], [DstName{i} '/' mat2str(DstPortNumber(i))]);
484                    end
485                end
486            end
487           
488            %%%%% SMWOWE %%%%%
489            if ~strcmp(get_param(SrcBlockHandleWE, 'block_type'), 'gatewayin') 
490                display(['A Gateway In is needed in order to connect to the web inport of the dpram ' dpramName '.']); 
491                return; 
492            end 
493            if ~strcmp(CompWEName(1:7), 'SMWOWE_')
494                display(['The Gateway In ', WEName ' needs to have the prefix SMWOWE_']);
495                return;
496            end
497            if ~strcmp(get_param(SrcBlockHandleWE, 'arith_type'), 'Boolean')
498                display(['The GatewayIn ' WEFullName ' needs to have an Output type of Boolean.']);
499                return;
500            end
501           
502            % Delete the GatewayIn
503            delete_block(SrcBlockHandleWE);
504
505            deleteSource(sys, cleanName(WEName));
506           
507            % Find the broken lines that starts from the output port of the
508            % deleted GatewayIn
509            brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
510
511            % Find out the handle of the port and the block
512            % These two handles will be used to extract more information
513            DstPortHandle = get_param(brokenlines, 'DstPortHandle');
514            DstBlockHandle = get_param(brokenlines, 'DstBlockHandle');
515
516            % From DstPortHandle, we can find out what is
517            % the destination port number
518            if length(DstPortHandle) > 1
519                DstPortNumber = cell2mat(get_param(DstPortHandle{length(DstPortHandle)}, 'PortNumber'));
520            else
521                DstPortNumber = get_param(DstPortHandle, 'PortNumber');
522            end
523
524            % From DstBlockHandle, we can find out what is the name of the
525            % destination block
526            if length(DstBlockHandle) > 1
527                DstName = get_param(DstBlockHandle{length(DstBlockHandle)}, 'Name');
528                try
529                    DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'block_type');
530                catch
531                    DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'BlockType');
532                end
533            else
534                DstName = get_param(DstBlockHandle, 'Name');
535                try
536                    DstBlockType = get_param(DstBlockHandle, 'block_type');
537                catch
538                    DstBlockType = get_param(DstBlockHandle, 'BlockType');
539                end
540            end
541
542            % Delete the borken lines
543            delete_line(brokenlines);
544
545            dpramWOWEIndex = dpramWOWEIndex + 1;
546            dpramWOWE{dpramWOWEIndex} = fromWE;
547            placeWE(1,1) = placeWE(1,1) - 60;
548            add_block('built-in/From', fromWE, 'GotoTag', ['SMWOWE_' int2str(dpramWOWEIndex - 1)], 'position', placeWE);
549           
550            FromName = get_param(fromWE, 'Name');
551            set_param(fromWE,'Orientation', orientationWE);
552
553            if size(DstName, 1) == 1
554                add_line(parentWE, [FromName '/1' ], [DstName '/' int2str(DstPortNumber)]);
555            else
556                for i=1:size(DstName, 1)
557                    add_line(parentWE, [FromName '/1'], [DstName{i} '/' mat2str(DstPortNumber(i))]);
558                end
559            end
560           
561
562        elseif strcmp(compName(1:5), 'SMRO_')       
563            dpramROIndex = dpramROIndex + 1;
564           
565            DstBlockHandleDataO = get_param(get_param(ports(4), 'Line'), 'DstBlockHandle');
566           
567           
568            %%%%%% SMROAddr %%%%%
569            if ~strcmp(CompAddrName(1:9), 'SMROAddr_')
570                display(['The Gateway In ' AddrName ' needs to have the prefix of SMROAddr_']);
571                return;
572            end
573           
574            % Delete the GatewayIn
575            delete_block(SrcBlockHandleAddr);
576
577            deleteSource(sys, cleanName(AddrName));
578           
579            % Find the broken lines that starts from the output port of the
580            % deleted GatewayIn
581            brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
582
583            % Find out the handle of the port and the block
584            % These two handles will be used to extract more information
585            DstPortHandle = get_param(brokenlines, 'DstPortHandle');
586            DstBlockHandle = get_param(brokenlines, 'DstBlockHandle');
587
588            % From DstPortHandle, we can find out what is
589            % the destination port number
590            if length(DstPortHandle) > 1
591                DstPortNumber = cell2mat(get_param(DstPortHandle{length(DstPortHandle)}, 'PortNumber'));
592            else
593                DstPortNumber = get_param(DstPortHandle, 'PortNumber');
594            end
595
596            % From DstBlockHandle, we can find out what is the name of the
597            % destination block
598            if length(DstBlockHandle) > 1
599                DstName = get_param(DstBlockHandle{length(DstBlockHandle)}, 'Name');
600                try
601                    DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'block_type');
602                catch
603                    DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'BlockType');
604                end
605            else
606                DstName = get_param(DstBlockHandle, 'Name');
607                try
608                    DstBlockType = get_param(DstBlockHandle, 'block_type');
609                catch
610                    DstBlockType = get_param(DstBlockHandle, 'BlockType');
611                end
612            end
613
614            % Delete the borken lines
615            delete_line(brokenlines);
616
617            dpramROAddrIndex = dpramROAddrIndex + 1;
618            dpramROAddr{dpramROAddrIndex} = fromAddr;
619            %placeAddr(1,1) = placeAddr(1,1) - 60;
620            placeAddr(1,3) = placeAddr(1,1) + 75;
621            placeAddrSlice = placeAddr;
622            placeAddrSlice(1,1) = placeAddrSlice(1,3) + 30;
623            placeAddrSlice(1,3) = placeAddrSlice(1,1) + 50;
624            add_block('built-in/From', fromAddr, 'GotoTag', ['SMROAddr_' int2str(dpramROAddrIndex - 1)], 'position', placeAddr);
625           
626            if(( version == 8.1) | (version == 8.2) | (version == 9.1))
627                add_block([spm '/Slice8'], fromAddrSlice, 'position', placeAddrSlice, 'nbits', fromAddrSliceBits);
628            end
629           
630            FromName = get_param(fromAddr, 'Name');
631            FromSliceName = get_param(fromAddrSlice, 'Name');
632            set_param(fromAddr,'Orientation', orientationAddr);
633
634            add_line(parentAddr, [FromName '/1' ], [FromSliceName '/1']);
635            if size(DstName, 1) == 1
636               
637                add_line(parentAddr, [FromSliceName '/1'], [DstName '/' int2str(DstPortNumber)]);
638                %add_line(parentAddr, [FromName '/1' ], [DstName '/' int2str(DstPortNumber)]);
639            else
640                for i=1:size(DstName, 1)
641                    if ~strcmp(DstName{i}, get_param(dpramName, 'Name'))
642                        add_line(parentAddr, [FromName '/1'], [DstName{i} '/' mat2str(DstPortNumber(i))]);
643                    else
644                        add_line(parentAddr, [FromSliceName '/1'], [DstName{i} '/' mat2str(DstPortNumber(i))]);
645                    end
646                end
647            end
648
649            if ~strcmp(get_param(SrcBlockHandleDataI, 'block_type'), 'constant')
650                display(['A Xilinx constant is needed in order to connect to the dinb inport of the dpram ' dpramName '.']);
651                return;
652            end
653            if ~strcmp(CompDataIName(1:10), 'SMRODataI_')
654                display(['The Xilinx block ' DataIName ' needs to have a prefix of SMRODataI_']);
655                return;
656            end
657            if (str2num(get_param(SrcBlockHandleDataI, 'const')) ~= 0) | ~strcmp(get_param(SrcBlockHandleDataI, 'n_bits'), '32')
658                display(['The Xilinx constant ' DataIFullName ' needs to have a constant value of zero. Number of bits needs to be 32.']);
659                return;
660            end
661
662            if ~strcmp(get_param(SrcBlockHandleWE, 'block_type'), 'constant')
663                display(['A Xilinx constant is needed in order to connect to the web inport of the dpram ' dpramName '.']);
664                return;
665            end
666            if ~strcmp(CompWEName(1:7), 'SMROWE_')
667                display(['The Gateway In ', WEName ' needs to have the prefix SMROWE_']);
668                return;
669            end
670            if (str2num(get_param(SrcBlockHandleWE, 'const')) ~=0) | (~strcmp(get_param(SrcBlockHandleWE, 'arith_type'), 'Boolean'))
671                display(['The Xilinx constant ' WEFullName ' needs to be a Boolean constant with a value of zero.']);
672                return;
673            end
674
675            %%%%% SMRODataO %%%%%
676            DataOName = get_param(DstBlockHandleDataO, 'Name');
677            DataONameIdx = 1;
678            if length(DstBlockHandleDataO) == 1
679                CompDataOName = [DataOName '          '];
680                DataOFullName = [get_param(DstBlockHandleDataO, 'Parent') '/' DataOName];
681            else
682                for i=1:length(DstBlockHandleDataO)
683                    CompDataOName{i} = [DataOName{i} '          '];
684                    DataOFullName{i} = [get_param(DstBlockHandleDataO(i), 'Parent') '/' DataOName{i}];                           
685                end
686            end
687           
688            IsGatewayOut = false;
689           
690            for d=1:length(DstBlockHandleDataO)
691                if strcmp(get_param(DstBlockHandleDataO(d), 'block_type'), 'gatewayout')
692                    IsGatewayOut = true;
693
694                    if length(DstBlockHandleDataO) == 1
695                        currentCompDataOName = CompDataOName;
696                    else
697                        currentCompDataOName = CompDataOName{d};
698                    end
699                       
700                    if ~strcmp(currentCompDataOName(1:10), 'SMRODataO_')
701                        display(['The Gateway Out ', DataOName ' needs to have a prefix of SMRODataO_']);
702                        return;
703                    end
704                    idxDataO = d;
705                end
706            end
707            if IsGatewayOut == false
708                display(['A GatewayOut is needed in order to connect to the B outport of the dpram ' dpramName '.']);
709                return;
710            end
711           
712            if length(DstBlockHandleDataO) > 1
713                DataOFullName = DataOFullName{idxDataO};
714            end
715           
716            DataOName = get_param(DataOFullName, 'Name');
717            orientationDataO = get_param(DataOFullName, 'Orientation');
718            placeDataO = get_param(DataOFullName, 'position');
719            parentDataO = get_param(DataOFullName, 'Parent');
720            gotoDataO = [parentDataO '/' cleanName(DataOName) '_sm' int2str(dpramWOROidx)];
721           
722            % Delete the block
723            delete_block(DataOFullName);
724           
725            deleteSink(sys, cleanName(DataOName));
726           
727            brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'DstBlockHandle', -1);
728
729            % Find out the handle of the port
730            % This will be used to extract the port number
731            SrcPortHandle = get_param(brokenlines, 'SrcPortHandle');
732            SrcPortNumber = get_param(SrcPortHandle, 'PortNumber');
733
734            % Find out the handle of the block
735            % This will be used to extract the name of the block
736            % This will also be used to extract the block type of the source
737            SrcBlockHandle = get_param(brokenlines, 'SrcBlockHandle');
738            SrcName = get_param(SrcBlockHandle, 'Name');
739            try
740                SrcBlockType = get_param(SrcBlockHandle, 'block_type');
741            catch
742                SrcBlockType = get_param(SrcBlockHandle, 'BlockType');
743            end
744
745            % Delete the broken line(s)
746            delete_line(brokenlines);
747           
748            dpramRODataOIndex = dpramRODataOIndex + 1;
749            dpramRODataO{dpramRODataOIndex} = gotoDataO;
750            placeDataO(1,3) = placeDataO(1,3) + 60;
751            add_block('built-in/Goto', gotoDataO, 'GotoTag', ['SMRODataO_' int2str(dpramRODataOIndex - 1)], 'TagVisibility', 'global', 'position', placeDataO);
752           
753             % Get the name of the Goto block
754            GotoName = get_param(gotoDataO, 'Name');
755
756            % Set the orientation
757            set_param(gotoDataO,'Orientation',orientationDataO);
758
759            % Connect a line from the source to the Goto block
760            add_line(parentDataO, [SrcName '/' int2str(SrcPortNumber)], [GotoName '/1']);
761           
762        end
763    end
764end
765
766gatewaysIn = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',   'block_type','fromreg');
767gatewaysOut = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',   'block_type','toreg');
768
769% Delete all broken lines
770brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
771delete_line(brokenlines);
772brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'DstBlockHandle', -1);
773delete_line(brokenlines);
774
775% gatesIn will hold the final list of all GatewaysIn
776% that are converted to registers
777gatesIn={}; 
778gatesIn_info = struct('arith_type',{},'nbits',{},'binpt',{},'initvalue',{});
779validGates = 0;
780fReg={};
781
782% Take every gateway (except the last one) and look at the
783% names after it to see if there is a repeat
784for c=1:length(gatewaysIn) 
785    % gateName will contain the full path of the gate
786    gateName = gatewaysIn{c};
787       
788    ftregsmName = get_param(gateName, 'shared_memory_name');
789    ftregsmName = ftregsmName(2:length(ftregsmName)-1);
790   
791    % shortName will contain only the name of the gate
792    shortName = get_param(gateName,'Name');
793   
794    %compName will be used to verify whether there is the prefix no_reg_
795    compName = [shortName '         '];
796   
797    if ~strcmp(compName(1:7), 'no_reg_')  %does this need to be a register?
798
799        % Find the orientation, position, and parent of the GatewayIn
800        orientation = get_param(gateName, 'Orientation');
801        place = get_param(gateName, 'position');
802        parent = get_param(gateName, 'Parent');
803
804        % Find the arith_type, number of bits, and bin_pt
805        % This will be stored into a struct
806        arithType = get_param(gateName, 'arith_type');
807        nBits = get_param(gateName, 'n_bits');
808        binPt = get_param(gateName, 'bin_pt');
809        initValue = '0';
810       
811        initValue = num2str(get_param(gateName, 'init'));
812
813        % from is the full path name of the From block
814        % from = [get_param(gateName,'Parent') '/' cleanName(get_param(gateName,'Name'))];
815        from = [get_param(gateName,'Parent') '/' cleanName(get_param(gateName,'Name')) '_opb' int2str(validGates)];
816       
817
818        % Delete the GatewayIn
819        delete_block(gateName);
820
821        % Find connected sources to the deleted GatewaysIn
822        % If there are sources that are connected to the GatewayIn, append
823        % the name of the gateway in to the source name
824        % Also check if the source block type is a constant
825        % If it is a constant, we will want to store the constant value
826        % into the struct gatesIn_info.
827        srcbrokenlines = find_system(sys, 'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'DstBlockHandle', -1);
828        if length(srcbrokenlines) > 0
829            % We have a source that was connected to the gateway in
830            % Get the handle of the source block
831            SrcBlockHandle = get_param(srcbrokenlines, 'SrcBlockHandle');
832            % Get the block type of the handle
833            SrcBlockType = get_param(SrcBlockHandle, 'BlockType');
834
835            % If the block type is a constant, we will want to store the
836            % constant value into initValue
837            if strcmp(SrcBlockType, 'Constant')
838                initValue = num2str(get_param(SrcBlockHandle, 'Value'));
839                %gatesIn_info = setfield(gatesIn_info, {validGates}, 'initvalue', num2str(get_param(SrcBlockHandle, 'Value')));
840            end
841
842            % Get the name of the source. The name of the GatewayIn will be
843            % appended to the name of the source
844            SrcName = get_param(SrcBlockHandle, 'name');
845            set_param(SrcBlockHandle, 'name', [SrcName '_' cleanName(shortName)]);
846            delete_line(srcbrokenlines);
847        end
848
849        % Find the broken lines that starts from the output port of the
850        % deleted GatewayIn
851        brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
852
853        % Find out the handle of the port and the block
854        % These two handles will be used to extract more information
855        DstPortHandle = get_param(brokenlines, 'DstPortHandle');
856        DstBlockHandle = get_param(brokenlines, 'DstBlockHandle');
857
858        % From DstPortHandle, we can find out what is
859        % the destination port number
860        if length(DstPortHandle) > 1
861            DstPortNumber = cell2mat(get_param(DstPortHandle{length(DstPortHandle)}, 'PortNumber'));
862        else
863            DstPortNumber = get_param(DstPortHandle, 'PortNumber');
864        end
865
866        % From DstBlockHandle, we can find otu what is the name of the
867        % destination block
868        if length(DstBlockHandle) > 1
869            DstName = get_param(DstBlockHandle{length(DstBlockHandle)}, 'Name');
870            try
871                DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'block_type');
872            catch
873                DstBlockType = get_param(DstBlockHandle{length(DstBlockHandle)}, 'BlockType');
874            end
875        else
876            DstName = get_param(DstBlockHandle, 'Name');
877            try
878                DstBlockType = get_param(DstBlockHandle, 'block_type');
879            catch
880                DstBlockType = get_param(DstBlockHandle, 'BlockType');
881            end
882        end
883
884        % Delete the borken lines
885        delete_line(brokenlines);
886
887        validGates = validGates + 1;
888        gatesIn_info = setfield(gatesIn_info, {validGates}, 'arith_type', arithType);
889        gatesIn_info = setfield(gatesIn_info, {validGates}, 'nbits', nBits);
890        gatesIn_info = setfield(gatesIn_info, {validGates}, 'binpt', binPt);
891        gatesIn_info = setfield(gatesIn_info, {validGates}, 'initvalue', initValue);
892        % gatesIn{validGates} = cleanName(gateName);
893        gatesIn{validGates} = cleanName([gateName '_opb' int2str(validGates - 1)]);
894
895        fReg{validGates} = cleanName([ftregsmName '_opb' int2str(validGates - 1)]);
896
897        add_block('built-in/From', from,'GotoTag', ['in_' int2str(validGates-1)], 'position', place);
898
899
900        FromName = get_param(from, 'Name');
901        set_param(from,'Orientation',orientation);
902
903        if size(DstName, 1) == 1
904            add_line(parent, [FromName '/1' ], [DstName '/' int2str(DstPortNumber)]);
905        else
906            for i=1:size(DstName, 1)                   
907                add_line(parent, [FromName '/1'], [DstName{i} '/' mat2str(DstPortNumber(i))]);
908            end
909        end
910
911        idx=0;  %appended to the end of a duplicate name
912        for d=c+1:length(gatewaysIn)
913            gateName2 = gatewaysIn{d};
914            shortName2 = get_param(gateName2,'Name');
915            if length(shortName2) < 7
916                compName2 = [shortName2 '         '];
917            else
918                compName2 = shortName2;
919            end
920           
921            if strcmp(cleanName(shortName),cleanName(shortName2)) | strcmp(cleanName(shortName), [appendString cleanName(compName2(8 : length(compName2)))])                               
922                set_param(gateName2,'Name', [shortName2 '_' int2str(idx)]);
923                gatewaysIn{d} = [gatewaysIn{d} '_' int2str(idx)];
924                idx=idx+1;
925            end
926        end
927    else
928        newGateName = [appendString shortName(8 : length(shortName))];
929       
930        idx=1;  %appended to the end of a duplicate name
931        for d=c+1:length(gatewaysIn)
932            gateName2 = gatewaysIn{d};
933            shortName2 = get_param(gateName2,'Name');
934            if length(shortName2) < 7
935                compName2 = [shortName2 '         '];
936            else
937                compName2 = shortName2;
938            end
939           
940            if strcmp(cleanName(newGateName),cleanName(shortName2)) | strcmp(cleanName(newGateName), [appendString cleanName(compName2(8 : length(compName2)))])               
941                if idx == 1
942                    set_param(gateName, 'Name', [newGateName '_' int2str(0)]);
943                end
944               
945                set_param(gateName2, 'Name', [shortName2 '_' int2str(idx)]);
946                gatewaysIn{d} = [gatewaysIn{d} '_' int2str(idx)];
947                idx = idx+1;
948            end
949        end
950        if idx == 1
951            set_param(gateName, 'Name', newGateName);   
952        end
953    end
954end
955
956
957% Delete all broken lines
958brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
959delete_line(brokenlines);
960brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'DstBlockHandle', -1);
961delete_line(brokenlines);
962
963gatesOut={}; %will hold final list of all gateways intented to map into registers
964validGates = 0;
965tReg={};
966
967for c=1:length(gatewaysOut)  %take every gateway (except the last one) and look at the names after it to see if there is a repeat
968    gateName = gatewaysOut{c};
969    shortName = get_param(gateName,'Name');
970    compName = [shortName '          '];
971   
972    ftregsmName = get_param(gateName, 'shared_memory_name');
973    ftregsmName = ftregsmName(2:length(ftregsmName)-1);
974   
975    hdlPortStatus = 1;
976   
977    if hdlPortStatus && ~strcmp(compName(1:7), 'no_reg_')  %does this need to be a register?
978       
979        % Get the orientation, position, and parent of the GatewayOut
980        orientation = get_param(gateName, 'Orientation');
981        place = get_param(gateName,'position');
982        parent = get_param(gateName, 'Parent');
983       
984        % goto is the full path name of the Goto block
985        % goto = [get_param(gateName,'Parent')  '/' cleanName(get_param(gateName,'Name'))];
986        goto = [get_param(gateName,'Parent')  '/' cleanName(get_param(gateName,'Name')) '_opb' int2str(validGates)];
987       
988        % Delete the block
989        delete_block(gateName);
990
991        % Find connected sinks to GatewayOut
992        % If there are sinks that are connected to the GatewayOut, append
993        % the name of the GatewayOut to the sink name
994        dstbrokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
995        if length(dstbrokenlines) > 0
996            % We have a sink that was connected to the GatewayOut
997            DstBlockHandle = get_param(dstbrokenlines, 'DstBlockHandle');
998
999            if length(DstBlockHandle) > 1
1000                for i=1:(length(DstBlockHandle) - 1)
1001                    DstName = get_param(DstBlockHandle{i}, 'Name');
1002                    set_param(DstBlockHandle{i}, 'name', [DstName '_' cleanName(shortName)]);
1003                end
1004                delete_line(dstbrokenlines);
1005            else
1006                DstName = get_param(DstBlockHandle, 'name');
1007                set_param(DstBlockHandle, 'name', [DstName '_' cleanName(shortName)]);
1008                delete_line(dstbrokenlines);
1009            end 
1010        end
1011
1012        brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'DstBlockHandle', -1);
1013
1014        validGates=validGates+1;
1015        % gatesOut{validGates} = cleanName(gateName);
1016        % reggoto = [parent  '/' cleanName(shortName)];
1017        % engoto = [parent '/enable_' cleanName(shortName)];
1018
1019        gatesOut{validGates} = cleanName([gateName '_opb' int2str(validGates-1)]);
1020
1021        tReg{validGates} = cleanName([ftregsmName '_opb' int2str(validGates - 1)]);
1022
1023        reggoto = [parent  '/' cleanName([shortName '_opb' int2str(validGates-1)])];
1024        engoto = [parent '/enable_' cleanName([shortName '_opb' int2str(validGates-1)])]; 
1025
1026        if length(brokenlines) == 2
1027            pos1 = get_param(brokenlines(1), 'points');
1028            pos2 = get_param(brokenlines(2), 'points');
1029
1030            SrcPortHandle1 = get_param(brokenlines(1), 'SrcPortHandle');
1031            SrcPortHandle2 = get_param(brokenlines(2), 'SrcPortHandle');
1032
1033            SrcPortNumber1 = get_param(SrcPortHandle1, 'PortNumber');
1034            SrcPortNumber2 = get_param(SrcPortHandle2, 'PortNumber');
1035
1036            SrcBlockHandle1 = get_param(brokenlines(1), 'SrcBlockHandle');
1037            SrcBlockHandle2 = get_param(brokenlines(2), 'SrcBlockHandle');
1038
1039            SrcName1 = get_param(SrcBlockHandle1, 'Name');
1040            SrcName2 = get_param(SrcBlockHandle2, 'Name');
1041
1042            DstPortHandle1 = get_param(brokenlines(1), 'DstPortHandle');
1043            DstPortHandle2 = get_param(brokenlines(2), 'DstPortHandle');
1044
1045            add_block('built-in/Goto', reggoto,'GotoTag', ['out_' int2str(length(gatesIn)+validGates-1)], 'TagVisibility', 'global', 'position', place);
1046            add_block('built-in/Goto', engoto,'GotoTag', ['en_out_' int2str(length(gatesIn)+validGates-1)], 'TagVisibility', 'global', 'position', place);
1047            set_param(reggoto,'Orientation', orientation);
1048            set_param(engoto, 'Orientation', orientation);
1049            regname = get_param(reggoto, 'Name');
1050            enname = get_param(engoto, 'Name');
1051
1052            delete_line(brokenlines(1));
1053            delete_line(brokenlines(2));
1054
1055            if strcmp(orientation, 'right') | strcmp(orientation, 'left')
1056                if pos1(length(pos1),2) < pos2(length(pos2),2)
1057                    add_line(parent, [SrcName1 '/' int2str(SrcPortNumber1)], [regname '/1']);
1058                    add_line(parent, [SrcName2 '/' int2str(SrcPortNumber2)], [enname '/1']);
1059                else
1060                    add_line(parent, [SrcName2 '/' int2str(SrcPortNumber2)], [regname '/1']);
1061                    add_line(parent, [SrcName1 '/' int2str(SrcPortNumber1)], [enname '/1']);
1062                end
1063            else
1064                if pos1(length(pos1),1) < pos2(length(pos2),1)
1065                    add_line(parent, [SrcName1 '/' int2str(SrcPortNumber1)], [regname '/1']);
1066                    add_line(parent, [SrcName2 '/' int2str(SrcPortNumber2)], [enname '/1']);
1067                else
1068                    add_line(parent, [SrcName2 '/' int2str(SrcPortNumber2)], [regname '/1']);
1069                    add_line(parent, [SrcName1 '/' int2str(SrcPortNumber1)], [enname '/1']);
1070                end
1071            end
1072        else
1073            disp('Error: Too many broken lines');
1074            return;
1075        end
1076
1077        idx=0;  %appended to the end of a duplicate name
1078        for d=c+1:length(gatewaysOut)
1079            gateName2 = gatewaysOut{d};
1080            shortName2 = get_param(gateName2,'Name');
1081            if length(shortName2) < 7
1082                compName2 = [shortName2 '         '];
1083            else
1084                compName2 = shortName2;
1085            end
1086           
1087            if strcmp(cleanName(shortName),cleanName(shortName2)) | strcmp(cleanName(shortName), [appendString cleanName(compName2(8 : length(compName2)))])
1088                set_param(gateName2,'Name', [shortName2 '_' int2str(idx)]);
1089                gatewaysOut{d} = [gatewaysOut{d} '_' int2str(idx)];
1090                idx=idx+1;
1091            end
1092        end
1093    elseif strcmp(compName(1:7), 'no_reg_')
1094        newGateName = [appendString shortName(8 : length(shortName))];
1095       
1096        idx=1;  %appended to the end of a duplicate name
1097        for d=c+1:length(gatewaysOut)
1098            gateName2 = gatewaysOut{d};
1099            shortName2 = get_param(gateName2,'Name');
1100            if length(shortName2) < 7
1101                compName2 = [shortName2 '         '];
1102            else
1103                compName2 = shortName2;
1104            end
1105           
1106            if strcmp(cleanName(newGateName),cleanName(shortName2)) | strcmp(cleanName(newGateName), [appendString cleanName(compName2(8 : length(compName2)))])               
1107                if idx == 1
1108                    set_param(gateName, 'Name', [newGateName '_' int2str(0)]);
1109                end
1110               
1111                set_param(gateName2, 'Name', [shortName2 '_' int2str(idx)]);
1112                gatewaysIn{d} = [gatewaysIn{d} '_' int2str(idx)];
1113                idx = idx+1;
1114            end
1115        end
1116        if idx == 1
1117            set_param(gateName, 'Name', newGateName);   
1118        end
1119    end
1120end
1121
1122% Check to make sure that we have at least one GatewayIn and one GatewayOut
1123% that will convert to registers
1124if length(gatesIn)==0 | length(gatesOut)==0
1125    close_system(sys, 0);
1126    display('Error: We need at least one From Register that will convert to register and one To Register that will convert to register');   
1127    return;
1128end
1129
1130% Delete all broken lines
1131brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
1132delete_line(brokenlines);
1133brokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'DstBlockHandle', -1);
1134delete_line(brokenlines);
1135
1136%   now to make the new i/o subsystem
1137script1 = get_param(sys,'preloadfcn');
1138script2 = get_param(sys,'initfcn');
1139script3 = get_param(sys,'startfcn');          %first get all of the information needed from the init scripts for the model
1140try
1141    run(script1);
1142    run(script2);
1143    run(script3);
1144catch
1145end
1146
1147% Add in the memory mapped IO from the skeleton model
1148try
1149    add_block([spm '/main8'], [sys '/Memory Mapped I_O']);
1150catch
1151    display('You already have converted this design to a peripheral.');
1152    display('Please get rid of the Memory Mapped I_O block and make sure')
1153    display('your gateways are all still in place before trying again.');
1154    return;
1155end
1156add_block([spm '/addr_from'], [sys '/addr_from']);
1157add_block([spm '/a_valid_from'], [sys '/a_valid_from']);
1158add_block([spm '/p_select8'], [sys '/p_select'], 'Position', [100 236 160 274]);
1159
1160add_block([spm '/ps_goto'], [sys '/ps_goto']);
1161add_line(sys, 'addr_from/1', 'p_select/1');
1162add_line(sys, 'a_valid_from/1', 'p_select/2');
1163add_line(sys, 'p_select/1', 'ps_goto/1');
1164ref = [sys '/Memory Mapped I_O'];
1165
1166set_param([sys '/p_select'], 'C_BASE', int2str(base)); %sets the base address according to the input parameter
1167%set_param([sys '/p_select'], 'C_HIGH', int2str(base+4*(length(gatesIn)+length(gatesOut)-1))); %sets the high address given number of inputs and outputs
1168try
1169    HighAddress =  base +(length(dpramWORO)+1)*hex2dec('10000') - 1;
1170catch
1171    HighAddress = base + hex2dec('FFFF');
1172end
1173set_param([sys '/p_select'], 'C_HIGH', int2str(HighAddress));
1174
1175if strcmp(smChoice, 'smon')
1176    for c=0:(length(dpramWODataI) - 1)
1177       goto = [ref  '/' 'SMWODataI_' int2str(c)];
1178       add_block('built-in/Goto', goto, 'GotoTag', ['SMWODataI_' int2str(c)], 'TagVisibility', 'global', 'position', place);
1179       add_line(ref, 'OPB2IP_IF/4', ['SMWODataI_' int2str(c) '/1']);
1180    end
1181end
1182
1183%takes care of OPB2IP_IF
1184subsys = [ref '/OPB2IP_IF'];
1185
1186if str2num(sysgenperiod) ~= 1
1187    set_param([subsys '/OPB_ABus'], 'period', sysgenperiod);
1188    set_param([subsys '/OPB_BE'], 'period', sysgenperiod);
1189    set_param([subsys '/OPB_DBus'], 'period', sysgenperiod);
1190    set_param([subsys '/OPB_RNW'], 'period', sysgenperiod);
1191    set_param([subsys '/OPB_Select'], 'period', sysgenperiod);
1192    set_param([subsys '/OPB_seqAddr'], 'period', sysgenperiod);
1193    set_param([subsys '/OPB_rst'], 'period', sysgenperiod);
1194end
1195
1196%takes care of the en_gen subsyst
1197%each subsequent address needs to be 4 bytes apart.
1198%After concatenating with the ack and rnw bits and slicing off the rest, we have a number that has
1199%2+log2(number of inputs and outputs) bits.
1200%the constant to compare with the address will need to be 2^(addr_width+1)
1201%+2^addr_width + addr  for reads and 2^(addr_width+1)+addr for writes
1202
1203subsys = [ref '/en_gen'];
1204addr_width = ceil(log2(length(gatesIn)+length(gatesOut)));
1205set_param([subsys '/Slice'], 'nbits', int2str(addr_width));
1206for c=0:length(gatesIn)-1
1207    writeRel = [subsys '/Write_rel_' int2str(c)];
1208    writeCon = [subsys '/Write_const_' int2str(c)];
1209    out = [subsys '/in_' int2str(c) '_we'];                                                %write enable output
1210    add_block([subsys '/given_relational'], writeRel);                                        %write relational
1211    add_block('built-in/Outport',out);
1212    add_block([subsys '/given_constant'], writeCon, 'n_bits', int2str(2+addr_width), 'const', int2str(2^(addr_width+1)+c));    %write constant
1213    add_line(subsys, 'Concat/1', [get_param(writeRel, 'Name') '/1']);
1214    add_line(subsys, [get_param(writeCon,'Name') '/1'], [get_param(writeRel,'Name') '/2']);   %attach second constant w/ second relational
1215    add_line(subsys, [get_param(writeRel,'Name') '/1'], [get_param(out,'Name') '/1']);
1216end
1217
1218if strcmp(smChoice, 'smon')
1219    if (length(dpramROAddr) + length(dpramWOAddr)) > 0
1220        add_block([subsys '/Slice'], [subsys '/AddrSlice' ], 'bit0', int2str(2), 'nbits', int2str(14));
1221        add_line(subsys, 'addr/1', ['AddrSlice/1']);
1222    end
1223    for c=0:(length(dpramROAddr) - 1)
1224        goto = [subsys '/' cleanName(get_param(dpramROAddr{c+1}, 'Name'))];
1225        add_block('built-in/Goto', goto, 'GotoTag', ['SMROAddr_' int2str(c)], 'TagVisibility', 'global');
1226        add_line(subsys, ['AddrSlice/1'], [get_param(goto, 'Name'), '/1']);
1227    end
1228    for c=0:(length(dpramWOAddr) - 1)
1229        goto = [subsys '/' cleanName(get_param(dpramWOAddr{c+1}, 'Name'))];
1230        add_block('built-in/Goto', goto, 'GotoTag', ['SMWOAddr_' int2str(c)], 'TagVisibility', 'global');
1231        add_line(subsys, ['AddrSlice/1'], [get_param(goto, 'Name'), '/1']);
1232    end
1233    if (length(dpramWOWE) > 0)
1234        add_block([subsys '/Slice'], [subsys '/BankSelect_slice'], 'nbits', int2str(4), 'bit0', int2str(16));
1235        add_block([subsys '/given_constant'], [subsys '/BaseAddress_const'], 'n_bits', int2str(32), 'bin_pt', int2str(0), 'const', int2str(base));
1236        add_block([subsys '/Slice'], [subsys '/BaseAddress_slice'], 'nbits', int2str(4), 'bit0', int2str(16));
1237        add_block([subsys '/given_sub'], [subsys '/addr_sub']);
1238        add_line(subsys, 'addr/1', 'BankSelect_slice/1');
1239        add_line(subsys, 'BaseAddress_const/1', 'BaseAddress_slice/1');
1240        add_line(subsys, 'BankSelect_slice/1', 'addr_sub/1');
1241        add_line(subsys, 'BaseAddress_slice/1', 'addr_sub/2');
1242
1243        add_block([subsys '/given_constant'], [subsys '/addr_const_' int2str(0)], 'n_bits', int2str(4), 'bin_pt', int2str(0), 'const', int2str(0));
1244        add_block([subsys '/given_relational'], [subsys '/addr_rel_' int2str(0)]);
1245        add_line(subsys, 'addr_sub/1', ['addr_rel_' int2str(0) '/1']);
1246        add_line(subsys, ['addr_const_' int2str(0) '/1'], ['addr_rel_' int2str(0) '/2']);
1247
1248        add_block([subsys '/given_inverter'], [subsys '/not_reg']);
1249        add_line(subsys, 'addr_rel_0/1', 'not_reg/1');
1250        delete_line(subsys, 'rnw/1', 'Concat/2');
1251        add_block([subsys, '/given_logical'], [subsys, '/or'], 'logical_function', 'OR', 'inputs', int2str(2));
1252        add_line(subsys, 'rnw/1', 'or/1');
1253        add_line(subsys, 'not_reg/1', 'or/2');
1254        add_line(subsys, 'or/1', 'Concat/2');
1255    end
1256
1257    for c=1:length(dpramWOWE)
1258        add_block([subsys '/given_constant'], [subsys '/addr_const_' int2str(c)], 'n_bits', int2str(4), 'bin_pt', int2str(0), 'const', int2str(length(dpramROAddr) + c));
1259        add_block([subsys '/given_relational'], [subsys '/addr_rel_' int2str(c)]);
1260        add_line(subsys, 'addr_sub/1', ['addr_rel_' int2str(c) '/1']);
1261        add_line(subsys, ['addr_const_' int2str(c) '/1'], ['addr_rel_' int2str(c) '/2']);
1262
1263        goto = [subsys '/' cleanName(get_param(dpramWOWE{c}, 'Name'))];
1264        add_block([subsys '/given_inverter'], [subsys '/not_SM_' int2str(c)]);
1265        add_block([subsys '/given_logical'], [subsys '/and_SM_' int2str(c)]);
1266        add_block('built-in/Goto', goto, 'GotoTag', ['SMWOWE_' int2str(c-1)], 'TagVisibility', 'global');
1267        add_line(subsys, 'rnw/1', ['not_SM_' int2str(c) '/1']);
1268        add_line(subsys, 'ack_in/1', ['and_SM_' int2str(c) '/1']);
1269        add_line(subsys, ['not_SM_' int2str(c) '/1'], ['and_SM_' int2str(c) '/2']);
1270        add_line(subsys, ['addr_rel_' int2str(c) '/1'], ['and_SM_' int2str(c) '/3']);
1271        add_line(subsys, ['and_SM_' int2str(c) '/1'], [get_param(goto, 'Name'), '/1']);
1272    end
1273end
1274
1275delete_block([subsys '/given_sub']);
1276delete_block([subsys '/given_logical']);
1277delete_block([subsys '/given_inverter']);
1278delete_block([subsys '/given_constant']);
1279delete_block([subsys '/given_relational']);
1280
1281
1282%takes care of the mem_if_in subsystem
1283subsys = [ref '/mem_if_in'];
1284for c=0:length(gatesIn)-1
1285    force = [subsys '/Reinterpret_' int2str(c)];
1286    cast = [subsys '/Convert_' int2str(c)];
1287   
1288    reg = [subsys '/' get_param(gatesIn{c+1}, 'Name') '_' int2str(c)];
1289    in = [subsys '/in_' int2str(c) '_we'];
1290    out = [subsys '/in_' int2str(c) '_data'];
1291    goto = [subsys '/' cleanName(get_param(gatesIn{c+1},'Name'))];
1292    atype = getfield(gatesIn_info,{c+1}, 'arith_type');
1293    nbits = getfield(gatesIn_info,{c+1}, 'nbits');
1294    binpt = getfield(gatesIn_info,{c+1}, 'binpt');
1295    initvalue = getfield(gatesIn_info, {c+1}, 'initvalue');
1296    if ~strcmp(atype, 'Boolean')     %gateway in not a boolean
1297        add_block([subsys '/given_reinterpret'], force, 'arith_type', atype, 'bin_pt', binpt);
1298        add_block([subsys '/given_convert'], cast, 'arith_type', atype, 'n_bits', nbits, 'bin_pt', binpt);
1299        add_line(subsys, [get_param(force, 'Name') '/1'], [get_param(cast, 'Name') '/1']);
1300    else 
1301        %gateway in is a boolean
1302        add_block([subsys '/given_reinterpret'], force, 'bin_pt', '0');
1303        add_block([subsys '/given_convert'], [subsys '/Conv_' int2str(c)], 'arith_type', 'Unsigned', 'n_bits', '1', 'bin_pt', '0');
1304        add_block([subsys '/given_convert'], cast, 'arith_type', 'Boolean');
1305        add_line(subsys, [get_param(force, 'Name') '/1'], ['Conv_' int2str(c) '/1']);
1306        add_line(subsys, ['Conv_' int2str(c) '/1'], [get_param(cast, 'Name') '/1']);
1307    end
1308    add_block([subsys '/given_register'], reg, 'init', initvalue);
1309    add_block('built-in/Inport', in);
1310    add_block('built-in/Outport', out);
1311    add_block('built-in/Goto', goto, 'GotoTag', ['in_' int2str(c)], 'TagVisibility', 'global');
1312    add_line(subsys, [get_param(reg, 'Name') '/1'], [get_param(goto, 'Name') '/1']);
1313    add_line(subsys, [get_param(cast,'Name') '/1'], [get_param(reg,'Name') '/1']);
1314    add_line(subsys, [get_param(reg,'Name') '/1'], [get_param(out,'Name') '/1']);
1315    add_line(subsys, [get_param(in,'Name') '/1'], [get_param(reg,'Name') '/2']);
1316    add_line(subsys, 'opb_dbus/1', [get_param(force, 'Name') '/1']);
1317   
1318end
1319delete_block([subsys '/given_convert']);
1320delete_block([subsys '/given_register']);
1321delete_block([subsys '/given_reinterpret']);
1322
1323
1324%takes care of the mem_if_out subsystem
1325subsys = [ref '/mem_if_out'];
1326
1327if strcmp(smChoice, 'smon') & dpramROIndex > 0
1328    set_param([subsys '/Delay'], 'latency', '1');
1329end
1330
1331set_param([subsys '/Slice'], 'nbits', int2str(addr_width));
1332gatesLeft = length(gatesIn)+length(gatesOut);
1333for c=0:length(gatesIn)-1
1334    dataIn = [subsys '/in_' int2str(c) '_data'];
1335    cast = [subsys '/cast_' int2str(c)];
1336    force = [subsys '/force_' int2str(c)];
1337    add_block('built-in/Inport', dataIn);
1338    add_block([subsys '/given_convert'], cast);
1339    atype = getfield(gatesIn_info,{c+1}, 'arith_type');
1340    if strcmp(atype, 'Boolean')     %gateway in is a boolean
1341        add_line(subsys, [get_param(dataIn, 'Name') '/1'], [get_param(cast, 'Name') '/1']);
1342    else
1343        add_block([subsys '/given_reinterpret'], force);
1344        add_line(subsys, [get_param(dataIn, 'Name') '/1'], [get_param(force, 'Name') '/1']);
1345        add_line(subsys, [get_param(force, 'Name') '/1'], [get_param(cast, 'Name') '/1']);
1346    end
1347end
1348for c = 0:length(gatesOut)-1
1349    from = [subsys '/' cleanName(get_param(gatesOut{c+1}, 'Name'))];
1350    register = [subsys '/' get_param(gatesOut{c+1}, 'Name') '_' int2str(length(gatesIn)+c)];
1351    cast = [subsys '/cast_' int2str(length(gatesIn)+c)];
1352    force = [subsys '/force_' int2str(length(gatesIn)+c)];
1353    enable = [subsys '/enable_' cleanName(get_param(gatesOut{c+1}, 'Name'))];
1354   
1355    add_block('built-in/From', from, 'GotoTag', ['out_' int2str(length(gatesIn)+c)]);
1356    add_block('built-in/From', enable, 'GotoTag', ['en_out_' int2str(length(gatesIn)+c)]);
1357    add_block([subsys '/given_register'], register, 'en', 'on');
1358
1359    add_block([subsys '/given_convert'], cast);
1360    add_block([subsys '/given_reinterpret'], force);
1361    add_line(subsys, [get_param(from, 'Name') '/1'], [get_param(register, 'Name') '/1']);
1362    add_line(subsys, [get_param(register, 'Name') '/1'], [get_param(force, 'Name') '/1']);
1363    add_line(subsys, [get_param(force, 'Name') '/1'], [get_param(cast, 'Name') '/1']);
1364    add_line(subsys, [get_param(enable, 'Name') '/1'], [get_param(register, 'Name') '/2']);
1365end
1366if strcmp(smChoice, 'smon')
1367    for c = 0:length(dpramRODataO) - 1
1368        from = [subsys '/' cleanName(get_param(dpramRODataO{c+1}, 'Name'))];
1369        add_block('built-in/From', from, 'GotoTag', ['SMRODataO_' int2str(c)]);
1370        if c==0
1371            mux = [subsys '/addrmux'];
1372            add_block([subsys '/given_mux'], mux, 'inputs', int2str(length(dpramRODataO) + 1), 'position', [10, 10, 55, 85+30*length(dpramRODataO)]);
1373            add_block([subsys '/Slice'], [subsys '/sel_slice'], 'bit0', int2str(16), 'nbits', int2str(length(dec2bin(length(dpramRODataO)))));
1374            add_block([subsys '/given_constant'], [subsys '/base_address'], 'bin_pt', '0', 'n_bits', int2str(32), 'const', int2str(base));
1375            add_block([subsys '/given_addsub'], [subsys '/const_sub'], 'n_bits', int2str(length(dec2bin(length(dpramRODataO)))));
1376            add_block([subsys '/Slice'], [subsys '/const_slice'], 'bit0', int2str(16), 'nbits', int2str(4));
1377            add_line(subsys, 'addr/1', 'sel_slice/1');
1378            add_line(subsys, 'base_address/1', 'const_slice/1');
1379            add_line(subsys, 'sel_slice/1', 'const_sub/1');
1380            add_line(subsys, 'const_slice/1', 'const_sub/2');
1381            add_line(subsys, 'const_sub/1', 'addrmux/1');
1382            add_line(subsys, 'Slice/1', 'addrmux/2');
1383        end
1384
1385        constant = [subsys '/SMconstant_' int2str(c)];
1386        add_block([subsys '/given_constant'], constant, 'const', int2str(c + length(gatesIn) + length(gatesOut)), 'bin_pt', '0', 'n_bits', int2str(length(dec2bin(c+length(gatesIn)+length(gatesOut)))));
1387           
1388        add_line(subsys, [get_param(constant, 'Name') '/1'], [get_param(mux, 'Name') '/' int2str(c+3)]);
1389    end
1390end
1391
1392%now we have all of the inputs and outputs going to a cast block, and we
1393%need to hook them up to the mux, sometimes requiring more than one layer
1394%of muxes
1395
1396if strcmp(smChoice, 'smon')
1397    outsInCurrentLayer = length(gatesIn)+length(gatesOut) + length(dpramRODataO);
1398    gwLeft = length(gatesIn) + length(gatesOut);
1399    dpramIndex = 0;
1400else
1401   outsInCurrentLayer = length(gatesIn)+length(gatesOut); 
1402end
1403
1404numberMuxes = ceil(outsInCurrentLayer / 32);
1405if numberMuxes > 1
1406    delay = get_param([subsys '/Delay'], 'latency');
1407    delay = str2num(delay) + 1;
1408    delay = set_param([subsys '/Delay'], 'latency', num2str(delay));
1409end
1410
1411outsLeft = outsInCurrentLayer;
1412muxNames = [];
1413nextIdx = 1;  %the index of the next cast block to be connected
1414begIdx = 0;  %the index of the first cast block in the current mux
1415while(outsLeft>0 & outsLeft~=33)
1416    mux = [subsys '/mux_' int2str(begIdx) '_' int2str(begIdx+min([outsLeft, 32])-1)];
1417    muxNames(nextIdx, 1:2) = [begIdx, begIdx+min([outsLeft, 32])-1];
1418    nextIdx = nextIdx+1;
1419    add_block([subsys '/given_mux'], mux, 'inputs',int2str(min([outsLeft, 32])), 'position', [10, 10, 55, 85+30*min([outsLeft, 32])]);
1420    for c=0:min([outsLeft, 32])-1
1421        if strcmp(smChoice, 'smon')
1422            if gwLeft > 0
1423                add_line(subsys, ['cast_' int2str(begIdx+c) '/1'], [get_param(mux,'Name') '/' int2str(c+2)]);
1424                gwLeft = gwLeft - 1;
1425            else
1426                dpramIndex = dpramIndex + 1;
1427                add_line(subsys, [get_param(dpramRODataO{dpramIndex}, 'Name') '/1'], [get_param(mux,'Name') '/' int2str(c+2)]);
1428                %add_line(subsys, ['SMconstant_' int2str(dpramIndex - 1) '/1'], ['addrmux/' int2str(dpramIndex+2)]);
1429            end
1430        else
1431            add_line(subsys, ['cast_' int2str(begIdx+c) '/1'], [get_param(mux,'Name') '/' int2str(c+2)]);
1432        end
1433    end
1434    addsub = [subsys '/a_correction_' int2str(muxNames(nextIdx-1,1))];
1435    const = [subsys '/const_' int2str(muxNames(nextIdx-1,1))];
1436    add_block([subsys '/given_constant'], const, 'const', int2str(muxNames(nextIdx-1,1)), 'n_bits', int2str(ceil(log2(2+muxNames(nextIdx-1,1)))));
1437    add_block([subsys '/given_addsub'], addsub, 'n_bits', int2str(ceil(log2(min([outsLeft,32])))));
1438    if strcmp(smChoice, 'smon')
1439        if (length(dpramRODataO) == 0)
1440            add_line(subsys, 'Slice/1', [get_param(addsub,'Name') '/1']);
1441        else
1442            add_line(subsys, 'addrmux/1', [get_param(addsub,'Name') '/1']);
1443        end
1444    else
1445        add_line(subsys, 'Slice/1', [get_param(addsub,'Name') '/1']);
1446    end
1447    add_line(subsys, [get_param(const,'Name') '/1'], [get_param(addsub,'Name') '/2']);
1448    add_line(subsys, [get_param(addsub,'Name') '/1'], [get_param(mux, 'Name') '/1']);
1449    begIdx = begIdx + min([outsLeft, 32]);
1450    outsLeft = outsLeft - min([outsLeft, 32]);
1451end
1452if outsLeft==33  %break it up into two last muxes
1453    mux1 = [subsys '/mux_' int2str(begIdx) '_' int2str(begIdx+15)];  %16 inputs
1454    muxNames(nextIdx, 1:2) = [begIdx, begIdx+15];
1455    mux2 = [subsys '/mux_' int2str(begIdx+16) '_' int2str(begIdx+32)];  %17 inputs
1456    muxNames(nextIdx+1, 1:2) = [begIdx+16, begIdx+32];
1457    nextIdx = nextIdx+2;
1458    add_block([subsys '/given_mux'], mux1, 'inputs',int2str(16), 'position', [10, 10, 55, 565]);
1459    add_block([subsys '/given_mux'], mux2, 'inputs',int2str(17), 'position', [10, 10, 55, 595]);
1460    for c=0:15
1461        if strcmp(smChoice, 'smon')
1462          if gwLeft > 0
1463                add_line(subsys, ['cast_' int2str(begIdx+c) '/1'], [get_param(mux1,'Name') '/' int2str(c+2)]);
1464                try
1465                    add_line(subsys, ['cast_' int2str(begIdx+16+c) '/1'], [get_param(mux2,'Name') '/' int2str(c+2)]);
1466                catch
1467                    dpramIndex = dpramIndex + 1;
1468                    add_line(subsys, [get_param(dpramRODataO{dpramIndex}, 'Name') '/1'], [get_param(mux2,'Name') '/' int2str(c+2)]);
1469                end
1470            else
1471                dpramIndex = dpramIndex + 1;
1472                add_line(subsys, [get_param(dpramRODataO{dpramIndex}, 'Name') '/1'], [get_param(mux2,'Name') '/' int2str(c+2)]);
1473            end
1474        else
1475            add_line(subsys, ['cast_' int2str(begIdx+c) '/1'], [get_param(mux1,'Name') '/' int2str(c+2)]);
1476            add_line(subsys, ['cast_' int2str(begIdx+16+c) '/1'], [get_param(mux2,'Name') '/' int2str(c+2)]);
1477        end
1478    end
1479
1480    if strcmp(smChoice, 'smon')
1481        if (gwLeft ==0)
1482            add_line(subsys, ['cast_' int2str(begIdx+32) '/1'], [get_param(mux2,'Name') '/' int2str(18)]);
1483        else
1484            dpramIndex = dpramIndex + 1;
1485            add_line(subsys, [get_param(dpramRODataO{dpramIndex}, 'Name') '/1'], [get_param(mux2,'Name') '/' int2str(18)]);
1486        end
1487    else
1488        add_line(subsys, ['cast_' int2str(begIdx+32) '/1'], [get_param(mux2,'Name') '/' int2str(18)]);
1489    end
1490   
1491    %connect first set of blocks to first mux
1492    addsub1 = [subsys '/a_correction_' int2str(muxNames(nextIdx-2,1))];
1493    const1 = [subsys '/const_' int2str(muxNames(nextIdx-2,1))];
1494    add_block([subsys '/given_constant'], const1, 'const', int2str(muxNames(nextIdx-2,1)), 'n_bits', int2str(ceil(log2(2+muxNames(nextIdx-2,1)))));
1495    add_block([subsys '/given_addsub'], addsub1, 'n_bits', '4');
1496    if strcmp(smChoice, 'smon')
1497        if (length(dpramRODataO) == 0)
1498            add_line(subsys, 'Slice/1', [get_param(addsub1,'Name') '/1']);
1499        else
1500            add_line(subsys, 'addrmux/1', [get_param(addsub1,'Name') '/1']);
1501        end
1502    else
1503        add_line(subsys, 'Slice/1', [get_param(addsub1,'Name') '/1']);
1504    end
1505    add_line(subsys, [get_param(const1,'Name') '/1'], [get_param(addsub1,'Name') '/2']);
1506    add_line(subsys, [get_param(addsub1,'Name') '/1'], [get_param(mux1, 'Name') '/1']);
1507    %connect second set of blocks to second mux
1508    addsub2 = [subsys '/a_correction_' int2str(muxNames(nextIdx-1,1))];
1509    const2 = [subsys '/const_' int2str(muxNames(nextIdx-1,1))];
1510    add_block([subsys '/given_constant'], const2, 'const', int2str(muxNames(nextIdx-1,1)), 'n_bits', int2str(ceil(log2(2+muxNames(nextIdx-1,1)))));
1511    add_block([subsys '/given_addsub'], addsub2, 'n_bits', '5');
1512    if strcmp(smChoice, 'smon')
1513        if (length(dpramRODataO) == 0)
1514            add_line(subsys, 'Slice/1', [get_param(addsub2,'Name') '/1']);
1515        else
1516            add_line(subsys, 'addrmux/1', [get_param(addsub2,'Name') '/1']);
1517        end
1518    else
1519        add_line(subsys, 'Slice/1', [get_param(addsub2,'Name') '/1']);
1520    end
1521    add_line(subsys, [get_param(const2,'Name') '/1'], [get_param(addsub2,'Name') '/2']);
1522    add_line(subsys, [get_param(addsub2,'Name') '/1'], [get_param(mux2, 'Name') '/1']);
1523    begIdx = begIdx + 33;
1524    outsLeft = outsLeft - 33;
1525end
1526%first layer is done
1527begIdx = 0;
1528outsInCurrentLayer = ceil(outsInCurrentLayer/32);
1529if outsInCurrentLayer>1  %do second layer
1530    set_param([subsys '/Delay'], 'latency', '1');     %delays the ack bit when there are two layers of muxes
1531    rom = [subsys '/addr_conv'];   %the rom converts the addr into proper select value for the new mux
1532    initValues = [];
1533    muxSelect = 0;
1534    for block = 1:nextIdx-1
1535        initValues = [initValues; muxSelect*ones(muxNames(block,2)-muxNames(block,1)+1,1)];     %creates the initial values of the rom
1536        muxSelect = muxSelect+1;
1537        set_param([subsys '/mux_' int2str(muxNames(block,1)) '_' int2str(muxNames(block,2))], 'latency', '1');   %goes back and sets latency = 1 for all of the first layer muxes
1538    end
1539   
1540    initValuesString = ['['];
1541    for c=1:length(initValues)
1542        initValuesString = [initValuesString int2str(initValues(c)) ' '];       %converts initial values into a string representing the vector
1543    end
1544    initValuesString(length(initValuesString)) = ']';
1545    set_param(rom, 'depth', int2str(length(gatesIn)+length(gatesOut)), 'initVector', initValuesString, 'n_bits', int2str(ceil(log2(initValues(length(initValues))+1))));
1546    if strcmp(smChoice, 'smon')
1547        if (length(dpramRODataO) == 0)
1548            add_line(subsys, 'Slice/1', 'addr_conv/1');
1549        else
1550            add_line(subsys, 'addrmux/1', 'addr_conv/1');
1551        end
1552    else
1553        add_line(subsys, 'Slice/1', 'addr_conv/1');
1554    end
1555    outsLeft = outsInCurrentLayer;
1556    mux = [subsys '/mux_' int2str(muxNames(begIdx+1,1)) '_' int2str(muxNames(begIdx+outsLeft,2))];
1557    add_block([subsys '/given_mux'], mux, 'inputs',int2str(outsLeft), 'position', [10, 10, 55, 85+30*outsLeft]);
1558    muxNames(nextIdx, 1:2) = [muxNames(begIdx+1,1), muxNames(begIdx+outsLeft,2)];
1559    nextIdx = nextIdx+1;
1560    for c=0:outsLeft-1
1561        add_line(subsys, ['mux_' int2str(muxNames(begIdx+1+c,1)) '_' int2str(muxNames(begIdx+1+c,2)) '/1'], [get_param(mux,'Name') '/' int2str(2+c)]);
1562    end
1563    add_line(subsys, 'addr_conv/1', [get_param(mux,'Name') '/1']);
1564else
1565    delete_block([subsys '/addr_conv']);
1566end
1567add_line(subsys, ['mux_' int2str(muxNames(nextIdx-1,1)) '_' int2str(muxNames(nextIdx-1,2)) '/1'], 'valid_data/1');
1568
1569
1570delete_block([subsys '/given_register']);
1571delete_block([subsys '/given_convert']);
1572delete_block([subsys '/given_addsub']);
1573delete_block([subsys '/given_constant']);
1574delete_block([subsys '/given_mux']);
1575delete_block([subsys '/given_reinterpret']);
1576
1577
1578%now attach all of the subsystems appropriately
1579for c=1:length(gatesIn)
1580    add_line(ref, ['en_gen/' int2str(c+2)], ['mem_if_in/' int2str(c+1)]);
1581    add_line(ref, ['mem_if_in/' int2str(c)], ['mem_if_out/' int2str(2+c)]);
1582end
1583
1584%makes text log of gateways that were removed
1585fid = fopen([sys '.h'],'w');
1586
1587fprintf(fid, '%s\n', ['#include "xbasic_types.h"']);
1588fprintf(fid, '%s\n', ['#include "xstatus.h"']);
1589fprintf(fid, '%s\n\n\n', ['#include "xio.h"']);
1590
1591fprintf(fid, '%s\n', ['#define ' upper(sys) '_OFFSET (0x00000000)']);
1592
1593fprintf(fid, '%s\n\n', '//Gateways In');
1594for c = 0:length(gatesIn)-1
1595    varName = cleanName(fReg{c+1});
1596
1597    opbLocation = findstr(varName, '_opb');
1598    if length(opbLocation) == 1
1599        varName = varName(1:opbLocation-1);
1600    else
1601        varName = varName(1:opbLocation(length(opbLocation))-1);
1602    end
1603   
1604    fprintf(fid, '%s\n', ['#define ' sys '_' varName '_OFFSET (' upper(sys) '_OFFSET+0x' dec2hex(4*c) ')']);
1605    %fprintf(fid, '%s\n', ['#define ' sys '_' varName ' (' upper(sys) '_BASEADDR+0x' dec2hex(4*c) ')']);
1606    fprintf(fid, '%s\n', ['//tag name: in_' int2str(c)]);
1607    fprintf(fid, '%s\n', ['//arith_type = ' getfield(gatesIn_info, {c+1}, 'arith_type')]);
1608    fprintf(fid, '%s\n', ['//num_bits = ' getfield(gatesIn_info, {c+1}, 'nbits')]);
1609    fprintf(fid, '%s\n', ['//bin_pt = ' getfield(gatesIn_info, {c+1}, 'binpt')]);
1610end
1611fprintf(fid, '\n%s\n\n', '//Gateways Out');
1612for c = 0:length(gatesOut)-1
1613    varName = cleanName(tReg{c+1});
1614
1615    opbLocation = findstr(varName, '_opb');
1616    if length(opbLocation) == 1
1617        varName = varName(1:opbLocation-1);
1618    elseif length(opbLocation) > 1
1619        varName = varName(1:opbLocation(length(opbLocation))-1);
1620    end
1621   
1622    fprintf(fid, '%s\n', ['#define ' sys '_' varName '_OFFSET (' upper(sys) '_OFFSET+0x' dec2hex(4*(length(gatesIn) + c)) ')']);
1623    %fprintf(fid, '%s\n', ['#define ' sys '_' varName ' (' upper(sys) '_BASEADDR+0x' dec2hex(4*(length(gatesIn) + c)) ')']);
1624    fprintf(fid, '%s\n', ['//tag name: out_' int2str(length(gatesIn)+c)]);
1625    %fprintf(fid, '%s\n', ['//arith_type = ' arith_type{1}]);
1626    %fprintf(fid, '%s\n', ['//num_bits = ' n_bits{1}]);
1627    %fprintf(fid, '%s\n', ['//bin_pt = ' bin_pt{1}]);
1628end
1629
1630if strcmp(smChoice, 'smon')
1631    fprintf(fid, '\n%s\n\n', '//Shared Memory Address');
1632    for c=0:length(dpramWORO) - 1
1633       
1634        varName = cleanName(dpramWORO{c+1});
1635        smLocation = findstr(varName, '_sm');
1636        if length(smLocation) == 1
1637            varName = varName(1:smLocation-1);
1638        elseif length(smLocation) > 1
1639            varName = varName(1:smLocation(length(smLocation))-1);
1640        end
1641       
1642        fprintf(fid, '%s\n', ['#define ' sys '_' varName '_OFFSET (' upper(sys) '_OFFSET+0x' dec2hex(c+1) '0000)']);
1643        %fprintf(fid, '%s\n\n', ['#define ' sys '_' varName ' (' upper(sys) '_BASEADDR+0x' dec2hex(c+1) '0000)']);
1644    end
1645end
1646
1647
1648
1649fprintf(fid, '\n\n%s', ['#define ' sys '_WriteReg(BaseAddress, RegOffset, Data) \']);
1650fprintf(fid, '\n\tXIo_Out32((BaseAddress) + (RegOffset), (Xuint32)(Data))');
1651
1652
1653fprintf(fid, '\n\n%s', ['#define ' sys '_ReadReg(BaseAddress, RegOffset) \']);
1654fprintf(fid, '\n\tXIo_In32((BaseAddress) + (RegOffset))');
1655
1656fprintf(fid, '\n\n%s\n\n', '//Write Reg Macro');
1657for c = 0:length(gatesIn)-1
1658    varName = cleanName(fReg{c+1});
1659
1660    opbLocation = findstr(varName, '_opb');
1661    if length(opbLocation) == 1
1662        varName = varName(1:opbLocation-1);
1663    else
1664        varName = varName(1:opbLocation(length(opbLocation))-1);
1665    end
1666   
1667    fprintf(fid, '%s\n', ['#define ' sys '_WriteReg_' varName '(BaseAddress, Value) \']);
1668    fprintf(fid, '\t%s\n', ['XIo_Out32((BaseAddress) + (' sys '_' varName '_OFFSET), (Xuint32)(Value))']);
1669end
1670for c = 0:length(gatesOut)-1
1671    varName = cleanName(tReg{c+1});
1672
1673    opbLocation = findstr(varName, '_opb');
1674    if length(opbLocation) == 1
1675        varName = varName(1:opbLocation-1);
1676    elseif length(opbLocation) > 1
1677        varName = varName(1:opbLocation(length(opbLocation))-1);
1678    end
1679
1680   
1681    fprintf(fid, '%s\n', ['#define ' sys '_WriteReg_' varName '(BaseAddress, Value) \']);
1682    fprintf(fid, '\t%s\n', ['XIo_Out32((BaseAddress) + (' sys '_' varName '_OFFSET), (Xuint32)(Value))']);
1683end
1684if strcmp(smChoice, 'smon')
1685    for c=0:length(dpramWORO) - 1
1686       
1687        varName = cleanName(dpramWORO{c+1});
1688        smLocation = findstr(varName, '_sm');
1689        if length(smLocation) == 1
1690            varName = varName(1:smLocation-1);
1691        elseif length(smLocation) > 1
1692            varName = varName(1:smLocation(length(smLocation))-1);
1693        end
1694       
1695        fprintf(fid, '%s\n', ['#define ' sys '_WriteReg_' varName '(BaseAddress, Value) \']);
1696        fprintf(fid, '\t%s\n', ['XIo_Out32((BaseAddress) + (' sys '_' varName '_OFFSET), (Xuint32)(Value))']);
1697    end
1698end
1699
1700fprintf(fid, '\n\n%s\n\n', '//Read Reg Macro');
1701for c = 0:length(gatesIn)-1
1702    varName = cleanName(fReg{c+1});
1703
1704    opbLocation = findstr(varName, '_opb');
1705    if length(opbLocation) == 1
1706        varName = varName(1:opbLocation-1);
1707    else
1708        varName = varName(1:opbLocation(length(opbLocation))-1);
1709    end
1710   
1711    fprintf(fid, '%s\n', ['#define ' sys '_ReadReg_' varName '(BaseAddress) \']);
1712    fprintf(fid, '\t%s\n', ['XIo_In32((BaseAddress) + (' sys '_' varName '_OFFSET))']);
1713end
1714for c = 0:length(gatesOut)-1
1715    varName = cleanName(tReg{c+1});
1716
1717    opbLocation = findstr(varName, '_opb');
1718    if length(opbLocation) == 1
1719        varName = varName(1:opbLocation-1);
1720    elseif length(opbLocation) > 1
1721        varName = varName(1:opbLocation(length(opbLocation))-1);
1722    end
1723   
1724    fprintf(fid, '%s\n', ['#define ' sys '_ReadReg_' varName '(BaseAddress) \']);
1725    fprintf(fid, '\t%s\n', ['XIo_In32((BaseAddress) + (' sys '_' varName '_OFFSET))']);
1726end
1727if strcmp(smChoice, 'smon')
1728    for c=0:length(dpramWORO) - 1
1729       
1730        varName = cleanName(dpramWORO{c+1});
1731        smLocation = findstr(varName, '_sm');
1732        if length(smLocation) == 1
1733            varName = varName(1:smLocation-1);
1734        elseif length(smLocation) > 1
1735            varName = varName(1:smLocation(length(smLocation))-1);
1736        end
1737       
1738        fprintf(fid, '%s\n', ['#define ' sys '_ReadReg_' varName '(BaseAddress) \']);
1739    fprintf(fid, '\t%s\n', ['XIo_In32((BaseAddress) + (' sys '_' varName '_OFFSET))']);
1740    end
1741end
1742
1743fclose(fid);
1744
1745
1746%clean up models
1747set_param([ref '/en_gen'], 'position', [10, 10, 100, 70+40*length(gatesIn)]);
1748set_param([ref '/mem_if_out'], 'position', [10, 10, 120, 70+40*length(gatesIn)]);
1749set_param([ref '/mem_if_in'], 'position', [10, 10, 120, 70+40*length(gatesIn)]);
1750xlTBUtils('Layout', struct('x_pitch',150,'sys',ref));
1751xlTBUtils('Layout',struct('sys',[ref '/en_gen']));
1752xlTBUtils('Layout', struct('sys', [ref '/mem_if_in']));
1753xlTBUtils('Layout', struct('x_pitch', 150, 'sys', [ref '/mem_if_out']));
1754
1755save_system(sys);
1756close_system(sys);
1757
1758function name = cleanName(name)
1759for c = 1:length(name)
1760    if name(c)==' '
1761        name(c)='_';
1762    end
1763end
1764
1765function deleteSink(sys, shortName)
1766    % Find connected sinks to GatewayOut
1767    % If there are sinks that are connected to the GatewayOut, append
1768    % the name of the GatewayOut to the sink name
1769    dstbrokenlines = find_system(sys,'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'SrcBlockHandle', -1);
1770    if length(dstbrokenlines) > 0
1771        % We have a sink that was connected to the GatewayOut
1772        DstBlockHandle = get_param(dstbrokenlines, 'DstBlockHandle');
1773
1774        if length(DstBlockHandle) > 1
1775            for i=1:(length(DstBlockHandle) - 1)
1776                DstName = get_param(DstBlockHandle{i}, 'Name');
1777                set_param(DstBlockHandle{i}, 'name', [DstName '_' cleanName(shortName)]);
1778            end
1779            delete_line(dstbrokenlines);
1780        else
1781            DstName = get_param(DstBlockHandle, 'name');
1782            set_param(DstBlockHandle, 'name', [DstName '_' cleanName(shortName)]);
1783            delete_line(dstbrokenlines);
1784        end
1785
1786    end
1787   
1788
1789function deleteSource(sys, shortName)
1790    % Find connected sources to the deleted GatewaysIn
1791    % If there are sources that are connected to the GatewayIn, append
1792    % the name of the gateway in to the source name
1793    % Also check if the source block type is a constant
1794    % If it is a constant, we will want to store the constant value
1795    % into the struct gatesIn_info.
1796    srcbrokenlines = find_system(sys, 'LookUnderMasks', 'all', 'FollowLinks', 'on',  'FindAll', 'on', 'type', 'line', 'DstBlockHandle', -1);
1797    if length(srcbrokenlines) > 0
1798        % We have a source that was connected to the gateway in
1799        % Get the handle of the source block
1800        SrcBlockHandle = get_param(srcbrokenlines, 'SrcBlockHandle');
1801        % Get the block type of the handle
1802        SrcBlockType = get_param(SrcBlockHandle, 'BlockType');
1803
1804        % If the block type is a constant, we will want to store the
1805        % constant value into initValue
1806        if strcmp(SrcBlockType, 'Constant')
1807            initValue = num2str(get_param(SrcBlockHandle, 'Value'));
1808            %gatesIn_info = setfield(gatesIn_info, {validGates}, 'initvalue', num2str(get_param(SrcBlockHandle, 'Value')));
1809        end
1810
1811        % Get the name of the source. The name of the GatewayIn will be
1812        % appended to the name of the source
1813        SrcName = get_param(SrcBlockHandle, 'name');
1814        set_param(SrcBlockHandle, 'name', [SrcName '_' cleanName(shortName)]);
1815        delete_line(srcbrokenlines);
1816    end
Note: See TracBrowser for help on using the repository browser.