summaryrefslogtreecommitdiff
path: root/Demux.m
blob: a95f08a8c6502394402781ac4c8a0b6b772b2bdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
classdef (StrictDefaults) Demux < serdes.SerdesAbstractSystemObject & TriggeredComponent
    % Demux     Demultiplexer
    %   obj = Demux returns a System Object, obj, that distributes the
    %   incoming sampled data to serveral outputs.
    %
    % Demux methods:
    %   step -  Distributes the incoming sampled data to several outputs
    %           using the digital clock input to determine when to update
    %           the outputs.
    %           [SampleOut,ClockOut] = step(obj,SampleIn,ClockIn)
    %
    % Demux properties:
    %   DemuxWidth     - Width of the output signal
    %   NumberOfClocks - Width of the input signal
    %   SymbolTime     - Symbol time of system
    %   SampleInterval - Uniform time step of the system

    %   Copyright 2021 The MathWorks, Inc.

    %#codegen

    properties (Nontunable)

        %Demux Width
        DemuxWidth = 32;

        %ADC Depth
        NumberOfClocks = 4;
    end
    properties (Hidden, SetAccess=private)
        %demux properties
        SampleBuffer        % Demux buffer
        SampleOutInternal   % Demux output
        DemuxChannelIndex   % Demux channel index
        ClockOutInternal    % Output clock
        OutputClockCounter  % Output clock counter
        ClockHalfPeriod     % Output clock half period, samples
        StartUpSyncFlag     % Flag to align with 0 degree phase at start up
    end

    properties (SetAccess = immutable, Nontunable, Hidden)
        IsLinear = true;
        IsTimeInvariant = true;
    end
    properties (Nontunable,Hidden)
        %Input Waveform Type
        %   Set the input wave type as one of 'Sample' | 'Impulse' |
        %   'Waveform'.  The default is 'Sample'.
        WaveType = 'Sample';
    end
    properties(Hidden, Constant)
        WaveTypeSet = matlab.system.StringSet({'Sample','Impulse','Waveform'});
    end

    methods
        % Constructor
        function obj = Demux(varargin)
            % Support name-value pair arguments when constructing object
            obj.BlockName = 'Demux';
            setProperties(obj,nargin,varargin{:})
        end
    end
    methods (Hidden)
        % The below methods, getAMIParameters, getAMIInputNames and
        % getAMIOutputNames are for use only within the serdesDesigner App
        % and will not influence the AMI parameters in Simulink whatsoever.
        function amiParameters = getAMIParameters(~)
            amiParameters = {};
        end
        function names = getAMIInputNames(~)
            names = {};
        end
        function names = getAMIOutputNames(~)
            names = {};
        end
    end
    methods (Access = protected, Hidden)
        function val = isSample(obj)
            val = strcmpi(obj.WaveType,'Sample');
        end
    end
    methods(Access = protected)
        %% Common functions
        function setupImpl(obj)

            setupClock(obj)

            % Demux-specific properties
            obj.SampleBuffer        = zeros(obj.DemuxWidth, 1);
            obj.SampleOutInternal   = zeros(obj.DemuxWidth, 1);
            obj.DemuxChannelIndex   =  1;
            obj.OutputClockCounter  =  0;
            obj.ClockOutInternal    = -1;
            obj.StartUpSyncFlag     = false;

            % Output clock half period, samples
            SamplesPerSymbol    = round(obj.SymbolTime/obj.SampleInterval);
            obj.ClockHalfPeriod = (SamplesPerSymbol * obj.DemuxWidth) / 2;
        end

        function validateInputsImpl(~,waveIn)
            validateattributes(waveIn,{'numeric'},{'finite'},'','waveIn');
        end

        function [SampleOut,ClockOut] = stepImpl(obj,SampleIn,varargin)
            %[SampleOut,ClockOut] = stepImpl(obj,SampleIn,ClockIn)

            if nargin==3
                ClockIn = varargin{1};
            else
                ClockIn = 0;
            end


            if isSample(obj)
                ClockStep(obj,ClockIn)

                % On clock rising edge
                if obj.PhaseRisingIndex > 0

                    % If demux is not sync'ed up then sync it to the first rising
                    % edge of 0 deg input clock phase
                    if (~obj.StartUpSyncFlag) && (obj.PhaseRisingIndex == 1)
                        obj.StartUpSyncFlag = true;
                    end

                    % If demux is sync'ed up
                    if obj.StartUpSyncFlag

                        % Before populating first demux position
                        if obj.DemuxChannelIndex == 1

                            % Release buffer to output
                            obj.SampleOutInternal = obj.SampleBuffer;

                            % Set output clock to +1; reset clock counter
                            obj.ClockOutInternal = +1;
                            obj.OutputClockCounter   =  0;

                        end % obj.DemuxChannelIndex == 1

                        % Place input sample corresponding to the phase with rising
                        % clock edge into the next position of the output buffer
                        obj.SampleBuffer(obj.DemuxChannelIndex) = SampleIn(obj.PhaseRisingIndex);

                        % Update output buffer pointer
                        obj.DemuxChannelIndex = mod(obj.DemuxChannelIndex, obj.DemuxWidth) + 1;

                    end % obj.StartUpSyncFlag

                end % obj.PhaseRisingIndex > 0

                % Increment output clock counter
                obj.OutputClockCounter = obj.OutputClockCounter + 1;

                % After half period, set output clock to -1
                if obj.OutputClockCounter > obj.ClockHalfPeriod
                    obj.ClockOutInternal = -1;
                end

                % Assign outputs
                SampleOut  = obj.SampleOutInternal;
                ClockOut = obj.ClockOutInternal;
            else
                % Assign outputs
                SampleOut  = SampleIn;
                ClockOut = 0;
            end

        end
        function [sz_1,sz_2] = getOutputSizeImpl(obj)
            % Return size for each output port
            sz_1 = [obj.DemuxWidth 1];
            sz_2 = [1 1];
        end
        function [c1,c2] = isOutputFixedSizeImpl(~)
            c1 = true;
            c2 = true;
        end
        function [dt1,dt2] = getOutputDataTypeImpl(obj)
            dt1 = propagatedInputDataType(obj,1);
            dt2 = propagatedInputDataType(obj,1);
        end
        function [c1,c2] = isOutputComplexImpl(~)
            c1 = false;
            c2 = false;
        end

        function resetImpl(~)
            % Initialize / reset discrete-state properties
        end

        %% Simulink functions
        function icon = getIconImpl(~)
            % Define icon for System block
            icon = "Demux";
        end
        function [name1,name2] = getInputNamesImpl(~)
            name1 = 'Samples';
            name2 = 'Clock';
        end
        function [name1,name2] = getOutputNamesImpl(~)
            name1 = 'Samples';
            name2 = sprintf('Demux\nClock');
        end
        function num = getNumInputsImpl(obj)
            if isSample(obj)
                num = 2;
            else
                num = 1;
            end
        end
    end
    methods(Static, Access=protected)
        function group = getPropertyGroupsImpl(~)
            % Define property section(s) for System block dialog
            group = matlab.system.display.SectionGroup(...
                'Title','Main',...
                'PropertyList',{'DemuxWidth','NumberOfClocks',...
                'SymbolTime','SampleInterval'});            
        end
    end
end