| 1 | # | ||
| 2 | # This file is part of the Perlilog project. | ||
| 3 | # | ||
| 4 | # Copyright (C) 2003, Eli Billauer | ||
| 5 | # | ||
| 6 | # This program is free software; you can redistribute it and/or modify | ||
| 7 | # it under the terms of the GNU General Public License as published by | ||
| 8 | # the Free Software Foundation; either version 2 of the License, or | ||
| 9 | # (at your option) any later version. | ||
| 10 | # | ||
| 11 | # This program is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the GNU General Public License | ||
| 17 | # along with this program; if not, write to the Free Software | ||
| 18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | # | ||
| 20 | # A copy of the license can be found in a file named "licence.txt", at the | ||
| 21 | # root directory of this project. | ||
| 22 | # | ||
| 23 | |||
| 24 | # Interface object for single master | ||
| 25 | sub attempt { | ||
| 26 | my $this = shift; | ||
| 27 | |||
| 28 | my @wbms = grep {ref eq 'wbm'} @_; | ||
| 29 | |||
| 30 | return undef unless ($#wbms == 0); # Single master, please | ||
| 31 | |||
| 32 | my @vars = grep {ref eq 'vars'} @_; | ||
| 33 | my @extras = (); | ||
| 34 | |||
| 35 | # @others is a list of ports that will be connected to the master. | ||
| 36 | # These are not necessarily Wishbone ports, but may contain to-be | ||
| 37 | # adapted ports. | ||
| 38 | my @others = grep {((ref ne 'vars') && (ref ne 'wbm')) } @_; | ||
| 39 | |||
| 40 | |||
| 41 | # If a vars port has a read-write-address label, it's not an "extra" | ||
| 42 | # but a port that should be mapped on the bus, and should therefore join | ||
| 43 | # @others, rather than @extras, which consists of "helping" wires. | ||
| 44 | |||
| 45 | foreach (@vars) { | ||
| 46 | my %l = $_->get('labels'); | ||
| 47 | if (grep /^(r|w|rw|wr)\d+$/, keys %l) { | ||
| 48 | push @others, $_; | ||
| 49 | } else { | ||
| 50 | push @extras, $_; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | return "Nothing to connect the master with" | ||
| 55 | unless (@others); | ||
| 56 | |||
| 57 | # This shortcut is good if the interface objects order is reversed in the | ||
| 58 | # registration. It works, but should not be used, because we tell | ||
| 59 | # wbsimple what to do. | ||
| 60 | # | ||
| 61 | # It shows, though, that a master class can dispatch other, simpler classes. | ||
| 62 | # | ||
| 63 | # if (($#others==0) && (ref($others[0]) eq 'wbs')) { | ||
| 64 | # return wbsimple->new(nick => 'cheat_simple_wb'); } | ||
| 65 | |||
| 66 | my $self = $this->new(nick => 'single_master_wb_controller'); | ||
| 67 | |||
| 68 | # We are going to need two properties on each of the @others objects | ||
| 69 | # in order to make things work. Make sure they are there. | ||
| 70 | |||
| 71 | my ($bits, $select); | ||
| 72 | |||
| 73 | foreach my $mate (@others) { | ||
| 74 | my $bits = $mate->get('wb_adr_bits'); | ||
| 75 | return "Missing \'wb_adr_bits\' property on ".$self->safewho($mate) | ||
| 76 | unless (defined $bits); | ||
| 77 | return "'wb_adr_bits\' should be a decimal number and not ".$self->prettyval($bits). | ||
| 78 | " on ".$self->safewho($mate) | ||
| 79 | unless ($bits =~ /^\d+$/); | ||
| 80 | |||
| 81 | my $select = $mate->get('wb_adr_select'); | ||
| 82 | return "Missing \'wb_adr_select\' property on ".$self->safewho($mate) | ||
| 83 | unless (defined $select); | ||
| 84 | return "'wb_adr_select\' should be a decimal number and not ".$self->prettyval($select). | ||
| 85 | " on ".$self->safewho($mate) | ||
| 86 | unless ($select =~ /^\d+$/); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | # Now we create ports to match with, and attempt to find some volunteer | ||
| 91 | # to do the matching. | ||
| 92 | |||
| 93 | # We begin with making a slave for the master port. | ||
| 94 | |||
| 95 | my $myport = wbs->new(nick => 'wb_bus_controller_slave_port', | ||
| 96 | parent => $self); | ||
| 97 | |||
| 98 | my @objs = $self->intobjects(@wbms, $myport, @extras); | ||
| 99 | return "Failed to match ".$self->safewho(@wbms). | ||
| 100 | " with the Wishbone given by the single-master bus controller generator\n" | ||
| 101 | unless (@objs); | ||
| 102 | my @bunch = ($self, $myport, @objs); | ||
| 103 | |||
| 104 | # Now we scan through the other ports. We just try to match them with | ||
| 105 | # single Wishbone masters. If they are not Wishbone slaves, they should | ||
| 106 | # be automatically adapted to such. | ||
| 107 | |||
| 108 | foreach (@others) { | ||
| 109 | $myport = wbm->new(nick => 'wb_bus_controller_master_port', | ||
| 110 | parent => $self); | ||
| 111 | |||
| 112 | @objs = $self->intobjects($_, $myport, @extras); | ||
| 113 | return "Failed to adapt ".$self->safewho($_)." to a Wishbone master, that was generated". | ||
| 114 | "by the single-master bus controller generator\n" | ||
| 115 | unless (@objs); | ||
| 116 | push @bunch, $myport, @objs; | ||
| 117 | } | ||
| 118 | |||
| 119 | return @bunch; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub generate { | ||
| 123 | my $self = shift; | ||
| 124 | |||
| 125 | # Get the ports to connect... | ||
| 126 | my @ports = $self->get('ports'); | ||
| 127 | my ($wbs) = grep {ref eq 'wbs'} @ports; | ||
| 128 | my @wbms = grep {ref eq 'wbm'} @ports; | ||
| 129 | |||
| 130 | # Find our target object (where we're going to write code to) | ||
| 131 | my $obj = $self->whereto; | ||
| 132 | |||
| 133 | # Now we create the necessary variables for our one slave port (the | ||
| 134 | # one connected with the master). | ||
| 135 | |||
| 136 | my %wbsNames = (); | ||
| 137 | my %wbsIDs = (); | ||
| 138 | |||
| 139 | ($wbsNames{'clk_i'}, $wbsIDs{'clk_i'}) = $obj->namevar('m_wb_clk_i', 'wire', 'in'); | ||
| 140 | ($wbsNames{'rst_i'}, $wbsIDs{'rst_i'}) = $obj->namevar('m_wb_rst_i', 'wire', 'in'); | ||
| 141 | ($wbsNames{'adr_i'}, $wbsIDs{'adr_i'}) = $obj->namevar('m_wb_adr_o', 'wire', 'in'); | ||
| 142 | ($wbsNames{'dat_i'}, $wbsIDs{'dat_i'}) = $obj->namevar('m_wb_dat_o', 'wire', 'in'); | ||
| 143 | ($wbsNames{'dat_o'}, $wbsIDs{'dat_o'}) = $obj->namevar('m_wb_dat_i', 'wire', 'out'); | ||
| 144 | ($wbsNames{'we_i'}, $wbsIDs{'we_i'}) = $obj->namevar('m_wb_we_o', 'wire', 'in'); | ||
| 145 | ($wbsNames{'stb_i'}, $wbsIDs{'stb_i'}) = $obj->namevar('m_wb_stb_o', 'wire', 'in'); | ||
| 146 | ($wbsNames{'cyc_i'}, $wbsIDs{'cyc_i'}) = $obj->namevar('m_wb_cyc_o', 'wire', 'in'); | ||
| 147 | ($wbsNames{'ack_o'}, $wbsIDs{'ack_o'}) = $obj->namevar('m_wb_ack_i', 'wire', 'out'); | ||
| 148 | |||
| 149 | $wbs->const('labels', %wbsIDs); # Set up the labels for the port. | ||
| 150 | |||
| 151 | # We are going to need to know the width of the main master's adr_o. Since | ||
| 152 | # the master Wishbone port existed before this object, it should be connected | ||
| 153 | # by now, and hence this property set. | ||
| 154 | |||
| 155 | my ($realmaster) = grep {ref eq 'wbm'} $self->get('perlilog-ports-to-connect'); | ||
| 156 | my %master_IDs = $self->labelID($realmaster); | ||
| 157 | my ($hi, $lo) = $self->bitrange($master_IDs{'adr_o'}); | ||
| 158 | |||
| 159 | wrong("Expected hi-to-low bit range on adr_o of ". | ||
| 160 | $realmaster->who()."\n") if ($lo > $hi); | ||
| 161 | |||
| 162 | wrong("Expected lowest bit in bit range on adr_o of ". | ||
| 163 | $realmaster->who()." to be zero\n") unless ($lo == 0); | ||
| 164 | |||
| 165 | # Now we copy off the variable names that should be "short-circuited" to the | ||
| 166 | # controller's slaves. The rest of the variables require a specific set-up, | ||
| 167 | # and will be handled per-case. | ||
| 168 | |||
| 169 | my %shorts = (clk_i => 'clk_i', | ||
| 170 | rst_i => 'rst_i', | ||
| 171 | stb_o => 'stb_i', | ||
| 172 | we_o => 'we_i', | ||
| 173 | dat_o => 'dat_i'); | ||
| 174 | |||
| 175 | my %mlabels = (); | ||
| 176 | foreach (keys %shorts) { | ||
| 177 | $mlabels{$_} = $wbsIDs{$shorts{$_}}; | ||
| 178 | } | ||
| 179 | |||
| 180 | # Finally, we set up the master port which mates with each | ||
| 181 | # of the slaves. | ||
| 182 | |||
| 183 | my ($bits, $select, $mate, $selcode); | ||
| 184 | my @acks = (); | ||
| 185 | my @dats = (); | ||
| 186 | |||
| 187 | my $main_ack = $wbsNames{'ack_o'}; | ||
| 188 | my $main_dat = $wbsNames{'dat_o'}; | ||
| 189 | my $main_cyc = $wbsNames{'cyc_i'}; | ||
| 190 | my $main_adr = $wbsNames{'adr_i'}; | ||
| 191 | |||
| 192 | foreach my $port (@wbms) { | ||
| 193 | # Find wb_adr_bits on the mating port | ||
| 194 | MATE: foreach $mate ($port->get('mates')) { | ||
| 195 | next MATE if (ref($mate) eq 'vars'); | ||
| 196 | $bits = $mate->get('wb_adr_bits'); | ||
| 197 | $select = $mate->get('wb_adr_select'); | ||
| 198 | last MATE if (defined $bits); | ||
| 199 | } | ||
| 200 | wrong("Failed to find \'wb_adr_bits\' property on any of ". | ||
| 201 | join(', ', map {$_->who} $port->get('mates'))."\n") | ||
| 202 | unless (defined $bits); | ||
| 203 | |||
| 204 | wrong("Found \'wb_adr_bits\' property, but not \'wb_adr_select\' on ". | ||
| 205 | $mate->who()."\n") | ||
| 206 | unless (defined $select); | ||
| 207 | |||
| 208 | wrong("Faulty \'wb_adr_bits\' property ".$self->prettyval($bits). | ||
| 209 | " on ".$mate->who()."\n") | ||
| 210 | unless ($bits =~ /^\d+$/); | ||
| 211 | |||
| 212 | wrong("Range exceeds dimensions, because \'wb_adr_bits\' on ". | ||
| 213 | $mate->who()." was too large\n") | ||
| 214 | if (($hi + 1) < $bits); | ||
| 215 | |||
| 216 | if ($hi < $bits) { | ||
| 217 | $selcode = "1"; | ||
| 218 | } else { | ||
| 219 | $selcode = $wbsNames{'adr_i'}.'['.$hi.':'.$bits.'] == '.$select; | ||
| 220 | } | ||
| 221 | |||
| 222 | my $adrhi = ($bits==0) ? $hi : ($bits - 1); | ||
| 223 | my ($adrvar, $adrID) = $obj->namevar('wb_slave_adr', 'wire', 'out', "[$adrhi:0]"); | ||
| 224 | $mlabels{'adr_o'} = $adrID; | ||
| 225 | |||
| 226 | my ($cycvar, $cycID) = $obj->namevar('wb_slave_cyc', 'wire', 'out'); | ||
| 227 | $mlabels{'cyc_o'} = $cycID; | ||
| 228 | |||
| 229 | $obj->samedim($cycvar, $main_cyc); | ||
| 230 | |||
| 231 | my ($ackvar, $ackID) = $obj->namevar('wb_slave_ack', 'wire', 'in'); | ||
| 232 | $mlabels{'ack_i'} = $ackID; | ||
| 233 | push @acks, $ackvar; | ||
| 234 | |||
| 235 | $obj->samedim($ackvar, $main_ack); | ||
| 236 | |||
| 237 | my $selvar = $obj->namevar('wb_slave_active', 'wire', 'out', ''); | ||
| 238 | $obj->append(" assign $selvar = $selcode;\n". | ||
| 239 | " assign $cycvar = $main_cyc & $selvar;\n". | ||
| 240 | " assign $adrvar = $main_adr".'['.$adrhi.":0];\n"); | ||
| 241 | |||
| 242 | my ($datvar, $datID) = $obj->namevar('wb_slave_dat', 'wire', 'in'); | ||
| 243 | $mlabels{'dat_i'} = $datID; | ||
| 244 | push @dats, "($selvar ? $datvar : 0)"; | ||
| 245 | |||
| 246 | $obj->samedim($datvar, $main_dat); | ||
| 247 | |||
| 248 | $port->const('labels', %mlabels); # Set up the labels for the port. | ||
| 249 | } | ||
| 250 | |||
| 251 | my $allacks = join(' | ', @acks); | ||
| 252 | my $alldats = join(' | ', @dats); | ||
| 253 | |||
| 254 | $obj->append(" assign $main_ack = $allacks;\n". | ||
| 255 | " assign $main_dat = $alldats;\n"); | ||
| 256 | |||
| 257 | # In a perfect world, we would check up here if the slave selects don't | ||
| 258 | # overlap because of addressing inconsintencies. | ||
| 259 | } |