├── LICENSE ├── README.md ├── rtl model ├── HuffmanCode.v ├── freqcount.v ├── huffman_top.v ├── para2ser.v └── sortnet │ ├── BitonicSortX4.v │ ├── BitonicSortX8.v │ ├── SortElement.v │ ├── SortX16.v │ ├── SortX4.v │ └── SortX8.v ├── simulation ├── huffman_test.v └── sortx16_sim.v └── software model └── HuffNode.cpp /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HuffmanCode -------------------------------------------------------------------------------- /rtl model/HuffmanCode.v: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | /* 3 | /* Description: 4 | /* main FSM control of huffman coding process 5 | /* Author: 6 | /* Guozhu Xin 7 | /* Date: 8 | /* 2017/7/27 9 | /* Email: 10 | /* spikexin@outlook.com 11 | ******************************************************************/ 12 | 13 | module HuffmanCode ( 14 | input clk, 15 | input rst_n, 16 | input [18:0] data_in0, 17 | input [18:0] data_in1, 18 | input [18:0] data_in2, 19 | input [18:0] data_in3, 20 | input [18:0] data_in4, 21 | input [18:0] data_in5, 22 | input [18:0] data_in6, 23 | input [18:0] data_in7, 24 | input [18:0] data_in8, 25 | input [18:0] data_in9, 26 | input req_coding, 27 | output reg ack_coding, 28 | output reg [8:0] data_out, 29 | output reg [3:0] data_len, 30 | output wire trans_start 31 | ); 32 | 33 | localparam IDLE = 2'd0; 34 | localparam CODING = 2'd1; 35 | localparam TRAVERSE = 2'd2; 36 | localparam OUTPUT = 2'd3; 37 | reg [1:0] state; // coding=0, traverse=1 38 | reg [1:0] next_state; 39 | reg [3:0] phase; 40 | reg [3:0] next_phase; 41 | reg [3:0] loop; 42 | reg [3:0] next_loop; 43 | 44 | reg [18:0] mem [0:18]; 45 | reg [18:0] a0; 46 | reg [18:0] a1; 47 | reg [18:0] a2; 48 | reg [18:0] a3; 49 | reg [18:0] a4; 50 | reg [18:0] a5; 51 | reg [18:0] a6; 52 | reg [18:0] a7; 53 | reg [18:0] a8; 54 | reg [18:0] a9; 55 | reg [18:0] a10; 56 | reg [18:0] a11; 57 | reg [18:0] a12; 58 | reg [18:0] a13; 59 | reg [18:0] a14; 60 | reg [18:0] a15; 61 | wire [18:0] sort0; 62 | wire [18:0] sort1; 63 | wire [18:0] sort2; 64 | wire [18:0] sort3; 65 | wire [18:0] sort4; 66 | wire [18:0] sort5; 67 | wire [18:0] sort6; 68 | wire [18:0] sort7; 69 | wire [18:0] sort8; 70 | wire [18:0] sort9; 71 | wire [18:0] sort10; 72 | wire [18:0] sort11; 73 | wire [18:0] sort12; 74 | wire [18:0] sort13; 75 | wire [18:0] sort14; 76 | wire [18:0] sort15; 77 | 78 | always @(posedge clk or negedge rst_n) begin 79 | if (~rst_n) 80 | ack_coding <= 1'b0; 81 | else if (req_coding) 82 | ack_coding <= 1'b1; 83 | else 84 | ack_coding <= 1'b0; 85 | end 86 | 87 | always @(posedge clk or negedge rst_n) begin 88 | if (~rst_n) begin 89 | state <= IDLE; 90 | phase <= 4'b0; 91 | loop <= 4'd0; 92 | end else begin 93 | state <= next_state; 94 | phase <= next_phase; 95 | loop <= next_loop; 96 | end 97 | end 98 | 99 | always @(*) begin 100 | case (state) 101 | IDLE: begin 102 | if (req_coding) 103 | next_state = CODING; 104 | else 105 | next_state = IDLE; 106 | next_phase = 4'b0; 107 | next_loop = 4'd0; 108 | end 109 | CODING: begin 110 | if (phase == 4'd9) begin 111 | next_state = TRAVERSE; 112 | next_phase = 4'd0; 113 | end else begin 114 | next_state = CODING; 115 | next_phase = phase + 1; 116 | end 117 | next_loop = 4'd0; 118 | end 119 | TRAVERSE: begin 120 | if (phase == 4'd8) begin 121 | next_state = OUTPUT; 122 | next_phase = 4'd0; 123 | next_loop = code_length[0]; 124 | end else begin 125 | next_state = TRAVERSE; 126 | next_phase = phase + 1; 127 | next_loop = 4'd0; 128 | end 129 | end 130 | OUTPUT: begin 131 | if (loop == 4'd1) begin 132 | if (phase == 4'd9) begin 133 | next_state = IDLE; 134 | next_phase = 4'd0; 135 | next_loop = 4'd0; 136 | end else begin 137 | next_state = OUTPUT; 138 | next_phase = phase + 1; 139 | next_loop = code_length[phase+1]; 140 | end 141 | end 142 | else begin 143 | next_state = OUTPUT; 144 | next_phase = phase; 145 | next_loop = loop - 1; 146 | end 147 | end 148 | default: begin 149 | next_state = state; 150 | next_phase = phase; 151 | next_loop = loop; 152 | end 153 | endcase 154 | end 155 | 156 | always @(posedge clk or negedge rst_n) begin 157 | if (~rst_n) begin 158 | a0 <= 19'b0; 159 | a1 <= 19'b0; 160 | a2 <= 19'b0; 161 | a3 <= 19'b0; 162 | a4 <= 19'b0; 163 | a5 <= 19'b0; 164 | a6 <= 19'b0; 165 | a7 <= 19'b0; 166 | a8 <= 19'b0; 167 | a9 <= 19'b0; 168 | a10 <= 19'b0; 169 | a11 <= 19'b0; 170 | a12 <= 19'b0; 171 | a13 <= 19'b0; 172 | a14 <= 19'b0; 173 | a15 <= 19'b0; 174 | end 175 | else if (state==CODING) begin 176 | case(phase) 177 | 4'd0: begin 178 | a0 <= data_in0; 179 | a1 <= data_in1; 180 | a2 <= data_in2; 181 | a3 <= data_in3; 182 | a4 <= data_in4; 183 | a5 <= data_in5; 184 | a6 <= data_in6; 185 | a7 <= data_in7; 186 | a8 <= data_in8; 187 | a9 <= data_in9; 188 | a10 <= 19'd255; 189 | a11 <= 19'd255; 190 | a12 <= 19'd255; 191 | a13 <= 19'd255; 192 | a14 <= 19'd255; 193 | a15 <= 19'd255; 194 | end 195 | 4'd1: begin 196 | a0 <= {6'b0, 5'd10, sort0[7:0]+sort1[7:0]}; 197 | a1 <= sort2; 198 | a2 <= sort3; 199 | a3 <= sort4; 200 | a4 <= sort5; 201 | a5 <= sort6; 202 | a6 <= sort7; 203 | a7 <= sort8; 204 | a8 <= sort9; 205 | a9 <= 19'd255; 206 | a10 <= 19'd255; 207 | a11 <= 19'd255; 208 | a12 <= 19'd255; 209 | a13 <= 19'd255; 210 | a14 <= 19'd255; 211 | a15 <= 19'd255; 212 | end 213 | 4'd2: begin 214 | a0 <= {6'b0, 5'd11, sort0[7:0]+sort1[7:0]}; 215 | a1 <= sort2; 216 | a2 <= sort3; 217 | a3 <= sort4; 218 | a4 <= sort5; 219 | a5 <= sort6; 220 | a6 <= sort7; 221 | a7 <= sort8; 222 | a8 <= 19'd255; 223 | a9 <= 19'd255; 224 | a10 <= 19'd255; 225 | a11 <= 19'd255; 226 | a12 <= 19'd255; 227 | a13 <= 19'd255; 228 | a14 <= 19'd255; 229 | a15 <= 19'd255; 230 | end 231 | 4'd3: begin 232 | a0 <= {6'b0, 5'd12, sort0[7:0]+sort1[7:0]}; 233 | a1 <= sort2; 234 | a2 <= sort3; 235 | a3 <= sort4; 236 | a4 <= sort5; 237 | a5 <= sort6; 238 | a6 <= sort7; 239 | a7 <= 19'd255; 240 | a8 <= 19'd255; 241 | a9 <= 19'd255; 242 | a10 <= 19'd255; 243 | a11 <= 19'd255; 244 | a12 <= 19'd255; 245 | a13 <= 19'd255; 246 | a14 <= 19'd255; 247 | a15 <= 19'd255; 248 | end 249 | 4'd4: begin 250 | a0 <= {6'b0, 5'd13, sort0[7:0]+sort1[7:0]}; 251 | a1 <= sort2; 252 | a2 <= sort3; 253 | a3 <= sort4; 254 | a4 <= sort5; 255 | a5 <= sort6; 256 | a6 <= 19'd255; 257 | a7 <= 19'd255; 258 | a8 <= 19'd255; 259 | a9 <= 19'd255; 260 | a10 <= 19'd255; 261 | a11 <= 19'd255; 262 | a12 <= 19'd255; 263 | a13 <= 19'd255; 264 | a14 <= 19'd255; 265 | a15 <= 19'd255; 266 | end 267 | 4'd5: begin 268 | a0 <= {6'b0, 5'd14, sort0[7:0]+sort1[7:0]}; 269 | a1 <= sort2; 270 | a2 <= sort3; 271 | a3 <= sort4; 272 | a4 <= sort5; 273 | a5 <= 19'd255; 274 | a6 <= 19'd255; 275 | a7 <= 19'd255; 276 | a8 <= 19'd255; 277 | a9 <= 19'd255; 278 | a10 <= 19'd255; 279 | a11 <= 19'd255; 280 | a12 <= 19'd255; 281 | a13 <= 19'd255; 282 | a14 <= 19'd255; 283 | a15 <= 19'd255; 284 | end 285 | 4'd6: begin 286 | a0 <= {6'b0, 5'd15, sort0[7:0]+sort1[7:0]}; 287 | a1 <= sort2; 288 | a2 <= sort3; 289 | a3 <= sort4; 290 | a4 <= 19'd255; 291 | a5 <= 19'd255; 292 | a6 <= 19'd255; 293 | a7 <= 19'd255; 294 | a8 <= 19'd255; 295 | a9 <= 19'd255; 296 | a10 <= 19'd255; 297 | a11 <= 19'd255; 298 | a12 <= 19'd255; 299 | a13 <= 19'd255; 300 | a14 <= 19'd255; 301 | a15 <= 19'd255; 302 | end 303 | 4'd7: begin 304 | a0 <= {6'b0, 5'd16, sort0[7:0]+sort1[7:0]}; 305 | a1 <= sort2; 306 | a2 <= sort3; 307 | a3 <= 19'd255; 308 | a4 <= 19'd255; 309 | a5 <= 19'd255; 310 | a6 <= 19'd255; 311 | a7 <= 19'd255; 312 | a8 <= 19'd255; 313 | a9 <= 19'd255; 314 | a10 <= 19'd255; 315 | a11 <= 19'd255; 316 | a12 <= 19'd255; 317 | a13 <= 19'd255; 318 | a14 <= 19'd255; 319 | a15 <= 19'd255; 320 | end 321 | 4'd8: begin 322 | a0 <= {6'b0, 5'd17, sort0[7:0]+sort1[7:0]}; 323 | a1 <= sort2; 324 | a2 <= 19'd255; 325 | a3 <= 19'd255; 326 | a4 <= 19'd255; 327 | a5 <= 19'd255; 328 | a6 <= 19'd255; 329 | a7 <= 19'd255; 330 | a8 <= 19'd255; 331 | a9 <= 19'd255; 332 | a10 <= 19'd255; 333 | a11 <= 19'd255; 334 | a12 <= 19'd255; 335 | a13 <= 19'd255; 336 | a14 <= 19'd255; 337 | a15 <= 19'd255; 338 | end 339 | 4'd9: begin 340 | a0 <= 19'b0; 341 | a1 <= 19'b0; 342 | a2 <= 19'b0; 343 | a3 <= 19'b0; 344 | a4 <= 19'b0; 345 | a5 <= 19'b0; 346 | a6 <= 19'b0; 347 | a7 <= 19'b0; 348 | a8 <= 19'b0; 349 | a9 <= 19'b0; 350 | a10 <= 19'b0; 351 | a11 <= 19'b0; 352 | a12 <= 19'b0; 353 | a13 <= 19'b0; 354 | a14 <= 19'b0; 355 | a15 <= 19'b0; 356 | end 357 | default: begin 358 | a0 <= 20'b0; 359 | a1 <= 20'b0; 360 | a2 <= 20'b0; 361 | a3 <= 20'b0; 362 | a4 <= 20'b0; 363 | a5 <= 20'b0; 364 | a6 <= 20'b0; 365 | a7 <= 20'b0; 366 | a8 <= 20'b0; 367 | a9 <= 20'b0; 368 | a10 <= 20'b0; 369 | a11 <= 20'b0; 370 | a12 <= 20'b0; 371 | a13 <= 20'b0; 372 | a14 <= 20'b0; 373 | a15 <= 20'b0; 374 | end 375 | endcase 376 | end 377 | else begin 378 | a0 <= 20'b0; 379 | a1 <= 20'b0; 380 | a2 <= 20'b0; 381 | a3 <= 20'b0; 382 | a4 <= 20'b0; 383 | a5 <= 20'b0; 384 | a6 <= 20'b0; 385 | a7 <= 20'b0; 386 | a8 <= 20'b0; 387 | a9 <= 20'b0; 388 | a10 <= 20'b0; 389 | a11 <= 20'b0; 390 | a12 <= 20'b0; 391 | a13 <= 20'b0; 392 | a14 <= 20'b0; 393 | a15 <= 20'b0; 394 | end 395 | end 396 | 397 | integer i; 398 | always @(posedge clk or negedge rst_n) begin 399 | if (~rst_n) begin 400 | for (i = 0; i < 19; i = i + 1) begin 401 | mem[i] <= 19'b0; 402 | end 403 | end 404 | else if (state == CODING) begin 405 | case (phase) 406 | 4'd0: ; 407 | 4'd1: begin 408 | mem[sort0[12:8]] <= {5'd10, 1'd0, sort0[12:0]}; 409 | mem[sort1[12:8]] <= {5'd10, 1'd1, sort1[12:0]}; 410 | end 411 | 4'd2: begin 412 | mem[sort0[12:8]] <= {5'd11, 1'd0, sort0[12:0]}; 413 | mem[sort1[12:8]] <= {5'd11, 1'd1, sort1[12:0]}; 414 | end 415 | 4'd3: begin 416 | mem[sort0[12:8]] <= {5'd12, 1'd0, sort0[12:0]}; 417 | mem[sort1[12:8]] <= {5'd12, 1'd1, sort1[12:0]}; 418 | end 419 | 4'd4: begin 420 | mem[sort0[12:8]] <= {5'd13, 1'd0, sort0[12:0]}; 421 | mem[sort1[12:8]] <= {5'd13, 1'd1, sort1[12:0]}; 422 | end 423 | 4'd5: begin 424 | mem[sort0[12:8]] <= {5'd14, 1'd0, sort0[12:0]}; 425 | mem[sort1[12:8]] <= {5'd14, 1'd1, sort1[12:0]}; 426 | end 427 | 4'd6: begin 428 | mem[sort0[12:8]] <= {5'd15, 1'd0, sort0[12:0]}; 429 | mem[sort1[12:8]] <= {5'd15, 1'd1, sort1[12:0]}; 430 | end 431 | 4'd7: begin 432 | mem[sort0[12:8]] <= {5'd16, 1'd0, sort0[12:0]}; 433 | mem[sort1[12:8]] <= {5'd16, 1'd1, sort1[12:0]}; 434 | end 435 | 4'd8: begin 436 | mem[sort0[12:8]] <= {5'd17, 1'd0, sort0[12:0]}; 437 | mem[sort1[12:8]] <= {5'd17, 1'd1, sort1[12:0]}; 438 | end 439 | 4'd9: begin 440 | mem[sort0[12:8]] <= {5'd18, 1'd0, sort0[12:0]}; 441 | mem[sort1[12:8]] <= {5'd18, 1'd1, sort1[12:0]}; 442 | mem[18] <= {5'd31, 1'd0, 5'd18, sort0[7:0]+sort1[7:0]}; // parent number 31 443 | end 444 | default: ; 445 | endcase 446 | end 447 | end 448 | 449 | SortX16 # ( 450 | .DSIZE (5'd19), 451 | .OFFSET (4'd8) 452 | ) sortx16_inst0 ( 453 | .a0 (a0), 454 | .a1 (a1), 455 | .a2 (a2), 456 | .a3 (a3), 457 | .a4 (a4), 458 | .a5 (a5), 459 | .a6 (a6), 460 | .a7 (a7), 461 | .a8 (a8), 462 | .a9 (a9), 463 | .a10 (a10), 464 | .a11 (a11), 465 | .a12 (a12), 466 | .a13 (a13), 467 | .a14 (a14), 468 | .a15 (a15), 469 | .sort0 (sort0), 470 | .sort1 (sort1), 471 | .sort2 (sort2), 472 | .sort3 (sort3), 473 | .sort4 (sort4), 474 | .sort5 (sort5), 475 | .sort6 (sort6), 476 | .sort7 (sort7), 477 | .sort8 (sort8), 478 | .sort9 (sort9), 479 | .sort10 (sort10), 480 | .sort11 (sort11), 481 | .sort12 (sort12), 482 | .sort13 (sort13), 483 | .sort14 (sort14), 484 | .sort15 (sort15) 485 | ); 486 | 487 | // traverse 488 | genvar j; 489 | reg [8:0] code_result [0:9]; 490 | reg [3:0] code_length [0:9]; 491 | reg [4:0] index [0:9]; 492 | 493 | generate 494 | for (j = 0; j < 10; j = j + 1) begin 495 | always @(posedge clk or negedge rst_n) begin 496 | if (~rst_n) begin 497 | code_result[j] <= 9'b0; 498 | code_length[j] <= 4'b0; 499 | index[j] <= 5'b0; 500 | end else if (state == TRAVERSE) begin 501 | case (phase) 502 | 4'd0: begin 503 | code_result[j][0] <= mem[j][13]; 504 | code_length[j] <= code_length[j] + 1; 505 | index[j] <= mem[j][18:14]; 506 | end 507 | 4'd1: begin 508 | if (index[j] != 5'd18) begin 509 | code_result[j][1] <= mem[index[j]][13]; 510 | code_length[j] <= code_length[j] + 1; 511 | index[j] <= mem[index[j]][18:14]; 512 | end 513 | end 514 | 4'd2: begin 515 | if (index[j] != 5'd18) begin 516 | code_result[j][2] <= mem[index[j]][13]; 517 | code_length[j] <= code_length[j] + 1; 518 | index[j] <= mem[index[j]][18:14]; 519 | end 520 | end 521 | 4'd3: begin 522 | if (index[j] != 5'd18) begin 523 | code_result[j][3] <= mem[index[j]][13]; 524 | code_length[j] <= code_length[j] + 1; 525 | index[j] <= mem[index[j]][18:14]; 526 | end 527 | end 528 | 4'd4: begin 529 | if (index[j] != 5'd18) begin 530 | code_result[j][4] <= mem[index[j]][13]; 531 | code_length[j] <= code_length[j] + 1; 532 | index[j] <= mem[index[j]][18:14]; 533 | end 534 | end 535 | 4'd5: begin 536 | if (index[j] != 5'd18) begin 537 | code_result[j][5] <= mem[index[j]][13]; 538 | code_length[j] <= code_length[j] + 1; 539 | index[j] <= mem[index[j]][18:14]; 540 | end 541 | end 542 | 4'd6: begin 543 | if (index[j] != 5'd18) begin 544 | code_result[j][6] <= mem[index[j]][13]; 545 | code_length[j] <= code_length[j] + 1; 546 | index[j] <= mem[index[j]][18:14]; 547 | end 548 | end 549 | 4'd7: begin 550 | if (index[j] != 5'd18) begin 551 | code_result[j][7] <= mem[index[j]][13]; 552 | code_length[j] <= code_length[j] + 1; 553 | index[j] <= mem[index[j]][18:14]; 554 | end 555 | end 556 | 4'd8: begin 557 | if (index[j] != 5'd18) begin 558 | code_result[j][8] <= mem[index[j]][13]; 559 | code_length[j] <= code_length[j] + 1; 560 | index[j] <= mem[index[j]][18:14]; 561 | end 562 | end 563 | default: begin 564 | code_result[j] <= 9'b0; 565 | code_length[j] <= 4'b0; 566 | index[j] <= 5'b0; 567 | end 568 | endcase 569 | end 570 | end 571 | end 572 | endgenerate 573 | 574 | always @(posedge clk or negedge rst_n) begin 575 | if (~rst_n) begin 576 | data_out <= 9'd0; 577 | data_len <= 4'd0; 578 | end 579 | else begin 580 | if (next_state == OUTPUT) begin 581 | data_out <= code_result[next_phase]; 582 | data_len <= code_length[next_phase]; 583 | end 584 | else begin 585 | data_out <= 9'd0; 586 | data_len <= 4'd0; 587 | end 588 | end 589 | end 590 | 591 | assign trans_start = (state == OUTPUT) ? 1'b1 : 1'b0; 592 | 593 | endmodule 594 | -------------------------------------------------------------------------------- /rtl model/freqcount.v: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | /* 3 | /* Description: 4 | /* count the frequency of each signal, 5 | /* for the huffman codeing processs afterwards 6 | /* Author: 7 | /* Guozhu Xin 8 | /* Date: 9 | /* 2017/7/11 10 | /* Email: 11 | /* spikexin@outlook.com 12 | ******************************************************************/ 13 | /* data_out bit domain 14 | /* | 18 --- 14 | 13 | 12 --- 8 | 7 --- 0 15 | /* | 0-18 | | 0-18 | 0-254 16 | /* | parent number | 0 or 1 |number (0-9)| frequency 17 | ******************************************************************/ 18 | /* 19 | 20 | NOTICE: 1.make sure the number of signals is less than 255 !! 21 | 2. the coding result may be different from the software, 22 | but they generate a same huffman tree,only the coding method 23 | is different 24 | 25 | ********************************************************************/ 26 | module freqcount( 27 | input clk, 28 | input rst_n, 29 | input start, // pulse signal 30 | input start_done, // along with the last data_in, pulse signal 31 | input [3:0] data_in, 32 | input ack_coding, // return signal of huffman coding module 33 | output reg req_coding, // request signal of huffman coding module 34 | output wire [18:0] data_out0, 35 | output wire [18:0] data_out1, 36 | output wire [18:0] data_out2, 37 | output wire [18:0] data_out3, 38 | output wire [18:0] data_out4, 39 | output wire [18:0] data_out5, 40 | output wire [18:0] data_out6, 41 | output wire [18:0] data_out7, 42 | output wire [18:0] data_out8, 43 | output wire [18:0] data_out9 44 | ); 45 | 46 | reg processing; 47 | 48 | always @(posedge clk or negedge rst_n) begin 49 | if (~rst_n) 50 | processing <= 1'b0; 51 | else if (start) 52 | processing <= 1'b1; 53 | else if (start_done) 54 | processing <= 1'b0; 55 | end 56 | 57 | reg [7:0] data_mem [0:9]; 58 | 59 | integer i; 60 | always @(posedge clk or negedge rst_n) begin 61 | if (~rst_n) begin 62 | for (i = 0; i < 10; i = i + 1) begin 63 | data_mem[i] <= 8'b0; 64 | end 65 | end else if (processing) begin 66 | case(data_in) 67 | 4'b0000: data_mem[0] <= data_mem[0] + 1; 68 | 4'b0001: data_mem[1] <= data_mem[1] + 1; 69 | 4'b0010: data_mem[2] <= data_mem[2] + 1; 70 | 4'b0011: data_mem[3] <= data_mem[3] + 1; 71 | 4'b0100: data_mem[4] <= data_mem[4] + 1; 72 | 4'b0101: data_mem[5] <= data_mem[5] + 1; 73 | 4'b0110: data_mem[6] <= data_mem[6] + 1; 74 | 4'b0111: data_mem[7] <= data_mem[7] + 1; 75 | 4'b1000: data_mem[8] <= data_mem[8] + 1; 76 | 4'b1001: data_mem[9] <= data_mem[9] + 1; 77 | default: ; 78 | endcase 79 | end 80 | end 81 | 82 | always @(posedge clk or negedge rst_n) begin 83 | if (~rst_n) 84 | req_coding <= 1'b0; 85 | else if (start_done) 86 | req_coding <= 1'b1; 87 | else if (ack_coding) 88 | req_coding <= 1'b0; 89 | end 90 | 91 | assign data_out0 = {6'b0, 5'd0, data_mem[0]}; 92 | assign data_out1 = {6'b0, 5'd1, data_mem[1]}; 93 | assign data_out2 = {6'b0, 5'd2, data_mem[2]}; 94 | assign data_out3 = {6'b0, 5'd3, data_mem[3]}; 95 | assign data_out4 = {6'b0, 5'd4, data_mem[4]}; 96 | assign data_out5 = {6'b0, 5'd5, data_mem[5]}; 97 | assign data_out6 = {6'b0, 5'd6, data_mem[6]}; 98 | assign data_out7 = {6'b0, 5'd7, data_mem[7]}; 99 | assign data_out8 = {6'b0, 5'd8, data_mem[8]}; 100 | assign data_out9 = {6'b0, 5'd9, data_mem[9]}; 101 | 102 | endmodule 103 | -------------------------------------------------------------------------------- /rtl model/huffman_top.v: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | /* 3 | /* Description: 4 | /* top file 5 | /* Author: 6 | /* Guozhu Xin 7 | /* Date: 8 | /* 2017/7/30 9 | /* Email: 10 | /* spikexin@outlook.com 11 | ******************************************************************/ 12 | 13 | module huffman_top( 14 | input clk, 15 | input rst_n, 16 | input start, // pulse signal 17 | input start_done, // along with the last data_in, pulse signal 18 | input [3:0] data_in, 19 | output wire output_data, // MSB first 20 | output wire output_start, 21 | output wire output_done 22 | ); 23 | 24 | wire ack_coding; 25 | wire req_coding; 26 | wire [18:0] data0; 27 | wire [18:0] data1; 28 | wire [18:0] data2; 29 | wire [18:0] data3; 30 | wire [18:0] data4; 31 | wire [18:0] data5; 32 | wire [18:0] data6; 33 | wire [18:0] data7; 34 | wire [18:0] data8; 35 | wire [18:0] data9; 36 | wire [8:0] data_para; 37 | wire [3:0] data_len; 38 | wire trans_start; 39 | 40 | freqcount freq_inst( 41 | .clk (clk), 42 | .rst_n (rst_n), 43 | .start (start), // pulse signal 44 | .start_done (start_done), // along with the last data_in, pulse signal 45 | .data_in (data_in), 46 | .ack_coding (ack_coding), // return signal of huffman coding module 47 | .req_coding (req_coding), // request signal of huffman coding module 48 | .data_out0 (data0), 49 | .data_out1 (data1), 50 | .data_out2 (data2), 51 | .data_out3 (data3), 52 | .data_out4 (data4), 53 | .data_out5 (data5), 54 | .data_out6 (data6), 55 | .data_out7 (data7), 56 | .data_out8 (data8), 57 | .data_out9 (data9) 58 | ); 59 | 60 | HuffmanCode huffmancode ( 61 | .clk (clk), 62 | .rst_n (rst_n), 63 | .data_in0 (data0), 64 | .data_in1 (data1), 65 | .data_in2 (data2), 66 | .data_in3 (data3), 67 | .data_in4 (data4), 68 | .data_in5 (data5), 69 | .data_in6 (data6), 70 | .data_in7 (data7), 71 | .data_in8 (data8), 72 | .data_in9 (data9), 73 | .req_coding (req_coding), 74 | .ack_coding (ack_coding), 75 | .data_out (data_para), 76 | .data_len (data_len), 77 | .trans_start(trans_start) 78 | ); 79 | 80 | para2ser para2ser_inst( 81 | .clk (clk), 82 | .rst_n (rst_n), 83 | .trans_start (trans_start), // indicate tansform begin, level signal 84 | .data (data_para), // signal:0-9 max coding length:9 85 | .data_len (data_len), // 9 - 1 86 | .output_data (output_data), // MSB first 87 | .output_start (output_start), 88 | .output_done (output_done) 89 | ); 90 | 91 | endmodule 92 | -------------------------------------------------------------------------------- /rtl model/para2ser.v: -------------------------------------------------------------------------------- 1 | /**************************************************************** 2 | /* 3 | /* Description: 4 | /* change internal parallel data to output 5 | /* serial data 6 | /* Author: 7 | /* Guozhu Xin 8 | /* Date: 9 | /* 2017/7/10 10 | /* Email: 11 | /* spikexin@outlook.com 12 | ******************************************************************/ 13 | 14 | module para2ser( 15 | input clk, 16 | input rst_n, 17 | input trans_start, // indicate tansform begin, level signal 18 | input [8:0] data, // signal:0-9 max coding length:9 19 | input [3:0] data_len, // 9 - 1 20 | output wire output_data, // MSB first 21 | output wire output_start, 22 | output wire output_done 23 | ); 24 | 25 | reg [3:0] data_cnt; 26 | reg [8:0] data_reg; 27 | 28 | always @(posedge clk or negedge rst_n) begin 29 | if(~rst_n) begin 30 | data_cnt <= 4'b0; 31 | data_reg <= 9'b0; 32 | end 33 | else if(trans_start) begin 34 | data_cnt <= (data_cnt == 4'b0) ? data_len-1 : data_cnt-1; 35 | data_reg <= data; 36 | end 37 | end 38 | 39 | reg trans_start_q1; 40 | reg trans_start_q2; 41 | always @(posedge clk or negedge rst_n) begin 42 | if (~rst_n) begin 43 | trans_start_q1 <= 1'b0; 44 | trans_start_q2 <= 1'b0; 45 | end 46 | else begin 47 | trans_start_q1 <= trans_start; 48 | trans_start_q2 <= trans_start_q1; 49 | end 50 | end 51 | 52 | assign output_start = trans_start & ~trans_start_q1; 53 | assign output_done = ~trans_start_q1 & trans_start_q2; 54 | assign output_data = (data_reg >> data_cnt) & 1'b1; 55 | 56 | endmodule 57 | -------------------------------------------------------------------------------- /rtl model/sortnet/BitonicSortX4.v: -------------------------------------------------------------------------------- 1 | module BitonicSortX4 # ( 2 | parameter DSIZE = 18, 3 | parameter OFFSET = 8 4 | )( 5 | input [DSIZE-1:0] a0, 6 | input [DSIZE-1:0] a1, 7 | input [DSIZE-1:0] a2, 8 | input [DSIZE-1:0] a3, 9 | output wire [DSIZE-1:0] sort0, 10 | output wire [DSIZE-1:0] sort1, 11 | output wire [DSIZE-1:0] sort2, 12 | output wire [DSIZE-1:0] sort3 13 | ); 14 | 15 | wire [DSIZE-1:0] sort0_0; 16 | wire [DSIZE-1:0] sort0_1; 17 | wire [DSIZE-1:0] sort1_0; 18 | wire [DSIZE-1:0] sort1_1; 19 | 20 | // half-clean 21 | SortElement # ( 22 | .DSIZE (DSIZE), 23 | .OFFSET(OFFSET) 24 | ) sort_inst0 ( 25 | .a(a0), 26 | .b(a2), 27 | .sort0(sort0_0), 28 | .sort1(sort0_1) 29 | ); 30 | SortElement # ( 31 | .DSIZE (DSIZE), 32 | .OFFSET(OFFSET) 33 | ) sort_inst1 ( 34 | .a(a1), 35 | .b(a3), 36 | .sort0(sort1_0), 37 | .sort1(sort1_1) 38 | ); 39 | 40 | // divide sort 41 | SortElement # ( 42 | .DSIZE (DSIZE), 43 | .OFFSET(OFFSET) 44 | ) sort_inst2 ( 45 | .a(sort0_0), 46 | .b(sort1_0), 47 | .sort0(sort0), 48 | .sort1(sort1) 49 | ); 50 | SortElement # ( 51 | .DSIZE (DSIZE), 52 | .OFFSET(OFFSET) 53 | ) sort_inst3 ( 54 | .a(sort0_1), 55 | .b(sort1_1), 56 | .sort0(sort2), 57 | .sort1(sort3) 58 | ); 59 | 60 | endmodule 61 | -------------------------------------------------------------------------------- /rtl model/sortnet/BitonicSortX8.v: -------------------------------------------------------------------------------- 1 | module BitonicSortX8 # ( 2 | parameter DSIZE = 18, 3 | parameter OFFSET = 8 4 | )( 5 | input [DSIZE-1:0] a0, 6 | input [DSIZE-1:0] a1, 7 | input [DSIZE-1:0] a2, 8 | input [DSIZE-1:0] a3, 9 | input [DSIZE-1:0] a4, 10 | input [DSIZE-1:0] a5, 11 | input [DSIZE-1:0] a6, 12 | input [DSIZE-1:0] a7, 13 | output wire [DSIZE-1:0] sort0, 14 | output wire [DSIZE-1:0] sort1, 15 | output wire [DSIZE-1:0] sort2, 16 | output wire [DSIZE-1:0] sort3, 17 | output wire [DSIZE-1:0] sort4, 18 | output wire [DSIZE-1:0] sort5, 19 | output wire [DSIZE-1:0] sort6, 20 | output wire [DSIZE-1:0] sort7 21 | ); 22 | 23 | wire [DSIZE-1:0] sort0_0; 24 | wire [DSIZE-1:0] sort0_1; 25 | wire [DSIZE-1:0] sort1_0; 26 | wire [DSIZE-1:0] sort1_1; 27 | wire [DSIZE-1:0] sort2_0; 28 | wire [DSIZE-1:0] sort2_1; 29 | wire [DSIZE-1:0] sort3_0; 30 | wire [DSIZE-1:0] sort3_1; 31 | 32 | // half-clean 33 | SortElement # ( 34 | .DSIZE (DSIZE), 35 | .OFFSET(OFFSET) 36 | ) sort_inst0 ( 37 | .a(a0), 38 | .b(a4), 39 | .sort0(sort0_0), 40 | .sort1(sort0_1) 41 | ); 42 | SortElement # ( 43 | .DSIZE (DSIZE), 44 | .OFFSET(OFFSET) 45 | ) sort_inst1 ( 46 | .a(a1), 47 | .b(a5), 48 | .sort0(sort1_0), 49 | .sort1(sort1_1) 50 | ); 51 | SortElement # ( 52 | .DSIZE (DSIZE), 53 | .OFFSET(OFFSET) 54 | ) sort_inst2 ( 55 | .a(a2), 56 | .b(a6), 57 | .sort0(sort2_0), 58 | .sort1(sort2_1) 59 | ); 60 | SortElement # ( 61 | .DSIZE (DSIZE), 62 | .OFFSET(OFFSET) 63 | ) sort_inst3 ( 64 | .a(a3), 65 | .b(a7), 66 | .sort0(sort3_0), 67 | .sort1(sort3_1) 68 | ); 69 | // divide sort 70 | BitonicSortX4 # ( 71 | .DSIZE (DSIZE), 72 | .OFFSET(OFFSET) 73 | ) bitonicsortx4_inst0 ( 74 | .a0 (sort0_0), 75 | .a1 (sort1_0), 76 | .a2 (sort2_0), 77 | .a3 (sort3_0), 78 | .sort0 (sort0), 79 | .sort1 (sort1), 80 | .sort2 (sort2), 81 | .sort3 (sort3) 82 | ); 83 | BitonicSortX4 # ( 84 | .DSIZE (DSIZE), 85 | .OFFSET(OFFSET) 86 | ) bitonicsortx4_inst1 ( 87 | .a0 (sort0_1), 88 | .a1 (sort1_1), 89 | .a2 (sort2_1), 90 | .a3 (sort3_1), 91 | .sort0 (sort4), 92 | .sort1 (sort5), 93 | .sort2 (sort6), 94 | .sort3 (sort7) 95 | ); 96 | 97 | endmodule 98 | -------------------------------------------------------------------------------- /rtl model/sortnet/SortElement.v: -------------------------------------------------------------------------------- 1 | module SortElement # ( 2 | parameter DSIZE = 18, 3 | parameter OFFSET = 8 4 | )( 5 | input [DSIZE-1:0] a, 6 | input [DSIZE-1:0] b, 7 | output wire [DSIZE-1:0] sort0, 8 | output wire [DSIZE-1:0] sort1 9 | ); 10 | 11 | assign sort0 = a[OFFSET-1:0] > b[OFFSET-1:0] ? b : a; 12 | assign sort1 = a[OFFSET-1:0] > b[OFFSET-1:0] ? a : b; 13 | 14 | endmodule 15 | -------------------------------------------------------------------------------- /rtl model/sortnet/SortX16.v: -------------------------------------------------------------------------------- 1 | module SortX16 # ( 2 | parameter DSIZE = 18, 3 | parameter OFFSET = 8 4 | )( 5 | input [DSIZE-1:0] a0, 6 | input [DSIZE-1:0] a1, 7 | input [DSIZE-1:0] a2, 8 | input [DSIZE-1:0] a3, 9 | input [DSIZE-1:0] a4, 10 | input [DSIZE-1:0] a5, 11 | input [DSIZE-1:0] a6, 12 | input [DSIZE-1:0] a7, 13 | input [DSIZE-1:0] a8, 14 | input [DSIZE-1:0] a9, 15 | input [DSIZE-1:0] a10, 16 | input [DSIZE-1:0] a11, 17 | input [DSIZE-1:0] a12, 18 | input [DSIZE-1:0] a13, 19 | input [DSIZE-1:0] a14, 20 | input [DSIZE-1:0] a15, 21 | output wire [DSIZE-1:0] sort0, 22 | output wire [DSIZE-1:0] sort1, 23 | output wire [DSIZE-1:0] sort2, 24 | output wire [DSIZE-1:0] sort3, 25 | output wire [DSIZE-1:0] sort4, 26 | output wire [DSIZE-1:0] sort5, 27 | output wire [DSIZE-1:0] sort6, 28 | output wire [DSIZE-1:0] sort7, 29 | output wire [DSIZE-1:0] sort8, 30 | output wire [DSIZE-1:0] sort9, 31 | output wire [DSIZE-1:0] sort10, 32 | output wire [DSIZE-1:0] sort11, 33 | output wire [DSIZE-1:0] sort12, 34 | output wire [DSIZE-1:0] sort13, 35 | output wire [DSIZE-1:0] sort14, 36 | output wire [DSIZE-1:0] sort15 37 | ); 38 | 39 | wire [DSIZE-1:0] sortX8_0_0; 40 | wire [DSIZE-1:0] sortX8_0_1; 41 | wire [DSIZE-1:0] sortX8_0_2; 42 | wire [DSIZE-1:0] sortX8_0_3; 43 | wire [DSIZE-1:0] sortX8_0_4; 44 | wire [DSIZE-1:0] sortX8_0_5; 45 | wire [DSIZE-1:0] sortX8_0_6; 46 | wire [DSIZE-1:0] sortX8_0_7; 47 | wire [DSIZE-1:0] sortX8_1_0; 48 | wire [DSIZE-1:0] sortX8_1_1; 49 | wire [DSIZE-1:0] sortX8_1_2; 50 | wire [DSIZE-1:0] sortX8_1_3; 51 | wire [DSIZE-1:0] sortX8_1_4; 52 | wire [DSIZE-1:0] sortX8_1_5; 53 | wire [DSIZE-1:0] sortX8_1_6; 54 | wire [DSIZE-1:0] sortX8_1_7; 55 | 56 | wire [DSIZE-1:0] sort0_0; 57 | wire [DSIZE-1:0] sort0_1; 58 | wire [DSIZE-1:0] sort1_0; 59 | wire [DSIZE-1:0] sort1_1; 60 | wire [DSIZE-1:0] sort2_0; 61 | wire [DSIZE-1:0] sort2_1; 62 | wire [DSIZE-1:0] sort3_0; 63 | wire [DSIZE-1:0] sort3_1; 64 | wire [DSIZE-1:0] sort4_0; 65 | wire [DSIZE-1:0] sort4_1; 66 | wire [DSIZE-1:0] sort5_0; 67 | wire [DSIZE-1:0] sort5_1; 68 | wire [DSIZE-1:0] sort6_0; 69 | wire [DSIZE-1:0] sort6_1; 70 | wire [DSIZE-1:0] sort7_0; 71 | wire [DSIZE-1:0] sort7_1; 72 | 73 | // divide sort 74 | SortX8 # ( 75 | .DSIZE (DSIZE), 76 | .OFFSET(OFFSET) 77 | ) sortX8_inst0 ( 78 | .a0 (a0), 79 | .a1 (a1), 80 | .a2 (a2), 81 | .a3 (a3), 82 | .a4 (a4), 83 | .a5 (a5), 84 | .a6 (a6), 85 | .a7 (a7), 86 | .sort0 (sortX8_0_0), 87 | .sort1 (sortX8_0_1), 88 | .sort2 (sortX8_0_2), 89 | .sort3 (sortX8_0_3), 90 | .sort4 (sortX8_0_4), 91 | .sort5 (sortX8_0_5), 92 | .sort6 (sortX8_0_6), 93 | .sort7 (sortX8_0_7) 94 | ); 95 | SortX8 # ( 96 | .DSIZE (DSIZE), 97 | .OFFSET(OFFSET) 98 | ) sortX8_inst1 ( 99 | .a0 (a8), 100 | .a1 (a9), 101 | .a2 (a10), 102 | .a3 (a11), 103 | .a4 (a12), 104 | .a5 (a13), 105 | .a6 (a14), 106 | .a7 (a15), 107 | .sort0 (sortX8_1_0), 108 | .sort1 (sortX8_1_1), 109 | .sort2 (sortX8_1_2), 110 | .sort3 (sortX8_1_3), 111 | .sort4 (sortX8_1_4), 112 | .sort5 (sortX8_1_5), 113 | .sort6 (sortX8_1_6), 114 | .sort7 (sortX8_1_7) 115 | ); 116 | 117 | // merge 118 | SortElement # ( 119 | .DSIZE (DSIZE), 120 | .OFFSET(OFFSET) 121 | ) sort_inst0 ( 122 | .a(sortX8_0_0), 123 | .b(sortX8_1_7), 124 | .sort0(sort0_0), 125 | .sort1(sort0_1) 126 | ); 127 | SortElement # ( 128 | .DSIZE (DSIZE), 129 | .OFFSET(OFFSET) 130 | ) sort_inst1 ( 131 | .a(sortX8_0_1), 132 | .b(sortX8_1_6), 133 | .sort0(sort1_0), 134 | .sort1(sort1_1) 135 | ); 136 | SortElement # ( 137 | .DSIZE (DSIZE), 138 | .OFFSET(OFFSET) 139 | ) sort_inst2 ( 140 | .a(sortX8_0_2), 141 | .b(sortX8_1_5), 142 | .sort0(sort2_0), 143 | .sort1(sort2_1) 144 | ); 145 | SortElement # ( 146 | .DSIZE (DSIZE), 147 | .OFFSET(OFFSET) 148 | ) sort_inst3 ( 149 | .a(sortX8_0_3), 150 | .b(sortX8_1_4), 151 | .sort0(sort3_0), 152 | .sort1(sort3_1) 153 | ); 154 | SortElement # ( 155 | .DSIZE (DSIZE), 156 | .OFFSET(OFFSET) 157 | ) sort_inst4 ( 158 | .a(sortX8_0_4), 159 | .b(sortX8_1_3), 160 | .sort0(sort4_0), 161 | .sort1(sort4_1) 162 | ); 163 | SortElement # ( 164 | .DSIZE (DSIZE), 165 | .OFFSET(OFFSET) 166 | ) sort_inst5 ( 167 | .a(sortX8_0_5), 168 | .b(sortX8_1_2), 169 | .sort0(sort5_0), 170 | .sort1(sort5_1) 171 | ); 172 | SortElement # ( 173 | .DSIZE (DSIZE), 174 | .OFFSET(OFFSET) 175 | ) sort_inst6 ( 176 | .a(sortX8_0_6), 177 | .b(sortX8_1_1), 178 | .sort0(sort6_0), 179 | .sort1(sort6_1) 180 | ); 181 | SortElement # ( 182 | .DSIZE (DSIZE), 183 | .OFFSET(OFFSET) 184 | ) sort_inst7 ( 185 | .a(sortX8_0_7), 186 | .b(sortX8_1_0), 187 | .sort0(sort7_0), 188 | .sort1(sort7_1) 189 | ); 190 | 191 | // bitonic 192 | BitonicSortX8 # ( 193 | .DSIZE (DSIZE), 194 | .OFFSET(OFFSET) 195 | ) bitonicsortx8_inst0 ( 196 | .a0 (sort0_0), 197 | .a1 (sort1_0), 198 | .a2 (sort2_0), 199 | .a3 (sort3_0), 200 | .a4 (sort4_0), 201 | .a5 (sort5_0), 202 | .a6 (sort6_0), 203 | .a7 (sort7_0), 204 | .sort0 (sort0), 205 | .sort1 (sort1), 206 | .sort2 (sort2), 207 | .sort3 (sort3), 208 | .sort4 (sort4), 209 | .sort5 (sort5), 210 | .sort6 (sort6), 211 | .sort7 (sort7) 212 | ); 213 | BitonicSortX8 # ( 214 | .DSIZE (DSIZE), 215 | .OFFSET(OFFSET) 216 | ) bitonicsortx8_inst1 ( 217 | .a0 (sort7_1), 218 | .a1 (sort6_1), 219 | .a2 (sort5_1), 220 | .a3 (sort4_1), 221 | .a4 (sort3_1), 222 | .a5 (sort2_1), 223 | .a6 (sort1_1), 224 | .a7 (sort0_1), 225 | .sort0 (sort8), 226 | .sort1 (sort9), 227 | .sort2 (sort10), 228 | .sort3 (sort11), 229 | .sort4 (sort12), 230 | .sort5 (sort13), 231 | .sort6 (sort14), 232 | .sort7 (sort15) 233 | ); 234 | 235 | endmodule 236 | -------------------------------------------------------------------------------- /rtl model/sortnet/SortX4.v: -------------------------------------------------------------------------------- 1 | module SortX4 # ( 2 | parameter DSIZE = 18, 3 | parameter OFFSET = 8 4 | )( 5 | input [DSIZE-1:0] a0, 6 | input [DSIZE-1:0] a1, 7 | input [DSIZE-1:0] a2, 8 | input [DSIZE-1:0] a3, 9 | output wire [DSIZE-1:0] sort0, 10 | output wire [DSIZE-1:0] sort1, 11 | output wire [DSIZE-1:0] sort2, 12 | output wire [DSIZE-1:0] sort3 13 | ); 14 | 15 | wire [DSIZE-1:0] sort0_0; 16 | wire [DSIZE-1:0] sort0_1; 17 | wire [DSIZE-1:0] sort1_0; 18 | wire [DSIZE-1:0] sort1_1; 19 | wire [DSIZE-1:0] sort2_0; 20 | wire [DSIZE-1:0] sort2_1; 21 | wire [DSIZE-1:0] sort3_0; 22 | wire [DSIZE-1:0] sort3_1; 23 | 24 | // divide sort 25 | SortElement # ( 26 | .DSIZE (DSIZE), 27 | .OFFSET(OFFSET) 28 | ) sort_inst0 ( 29 | .a(a0), 30 | .b(a1), 31 | .sort0(sort0_0), 32 | .sort1(sort0_1) 33 | ); 34 | SortElement # ( 35 | .DSIZE (DSIZE), 36 | .OFFSET(OFFSET) 37 | ) sort_inst1 ( 38 | .a(a2), 39 | .b(a3), 40 | .sort0(sort1_0), 41 | .sort1(sort1_1) 42 | ); 43 | 44 | // merge 45 | SortElement # ( 46 | .DSIZE (DSIZE), 47 | .OFFSET(OFFSET) 48 | ) sort_inst2 ( 49 | .a(sort0_0), 50 | .b(sort1_1), 51 | .sort0(sort2_0), 52 | .sort1(sort2_1) 53 | ); 54 | SortElement # ( 55 | .DSIZE (DSIZE), 56 | .OFFSET(OFFSET) 57 | ) sort_inst3 ( 58 | .a(sort0_1), 59 | .b(sort1_0), 60 | .sort0(sort3_0), 61 | .sort1(sort3_1) 62 | ); 63 | 64 | SortElement # ( 65 | .DSIZE (DSIZE), 66 | .OFFSET(OFFSET) 67 | ) sort_inst4 ( 68 | .a(sort2_0), 69 | .b(sort3_0), 70 | .sort0(sort0), 71 | .sort1(sort1) 72 | ); 73 | SortElement # ( 74 | .DSIZE (DSIZE), 75 | .OFFSET(OFFSET) 76 | ) sort_inst5 ( 77 | .a(sort3_1), 78 | .b(sort2_1), 79 | .sort0(sort2), 80 | .sort1(sort3) 81 | ); 82 | 83 | endmodule 84 | -------------------------------------------------------------------------------- /rtl model/sortnet/SortX8.v: -------------------------------------------------------------------------------- 1 | module SortX8 # ( 2 | parameter DSIZE = 18, 3 | parameter OFFSET = 8 4 | )( 5 | input [DSIZE-1:0] a0, 6 | input [DSIZE-1:0] a1, 7 | input [DSIZE-1:0] a2, 8 | input [DSIZE-1:0] a3, 9 | input [DSIZE-1:0] a4, 10 | input [DSIZE-1:0] a5, 11 | input [DSIZE-1:0] a6, 12 | input [DSIZE-1:0] a7, 13 | output wire [DSIZE-1:0] sort0, 14 | output wire [DSIZE-1:0] sort1, 15 | output wire [DSIZE-1:0] sort2, 16 | output wire [DSIZE-1:0] sort3, 17 | output wire [DSIZE-1:0] sort4, 18 | output wire [DSIZE-1:0] sort5, 19 | output wire [DSIZE-1:0] sort6, 20 | output wire [DSIZE-1:0] sort7 21 | ); 22 | 23 | wire [DSIZE-1:0] sortx4_0_0; 24 | wire [DSIZE-1:0] sortx4_0_1; 25 | wire [DSIZE-1:0] sortx4_0_2; 26 | wire [DSIZE-1:0] sortx4_0_3; 27 | wire [DSIZE-1:0] sortx4_1_0; 28 | wire [DSIZE-1:0] sortx4_1_1; 29 | wire [DSIZE-1:0] sortx4_1_2; 30 | wire [DSIZE-1:0] sortx4_1_3; 31 | 32 | wire [DSIZE-1:0] sort0_0; 33 | wire [DSIZE-1:0] sort0_1; 34 | wire [DSIZE-1:0] sort1_0; 35 | wire [DSIZE-1:0] sort1_1; 36 | wire [DSIZE-1:0] sort2_0; 37 | wire [DSIZE-1:0] sort2_1; 38 | wire [DSIZE-1:0] sort3_0; 39 | wire [DSIZE-1:0] sort3_1; 40 | 41 | // divide sort 42 | SortX4 # ( 43 | .DSIZE (DSIZE), 44 | .OFFSET(OFFSET) 45 | ) sortx4_inst0 ( 46 | .a0 (a0), 47 | .a1 (a1), 48 | .a2 (a2), 49 | .a3 (a3), 50 | .sort0 (sortx4_0_0), 51 | .sort1 (sortx4_0_1), 52 | .sort2 (sortx4_0_2), 53 | .sort3 (sortx4_0_3) 54 | ); 55 | SortX4 # ( 56 | .DSIZE (DSIZE), 57 | .OFFSET(OFFSET) 58 | ) sortx4_inst1 ( 59 | .a0 (a4), 60 | .a1 (a5), 61 | .a2 (a6), 62 | .a3 (a7), 63 | .sort0 (sortx4_1_0), 64 | .sort1 (sortx4_1_1), 65 | .sort2 (sortx4_1_2), 66 | .sort3 (sortx4_1_3) 67 | ); 68 | 69 | // merge 70 | SortElement # ( 71 | .DSIZE (DSIZE), 72 | .OFFSET(OFFSET) 73 | ) sort_inst0 ( 74 | .a(sortx4_0_0), 75 | .b(sortx4_1_3), 76 | .sort0(sort0_0), 77 | .sort1(sort0_1) 78 | ); 79 | SortElement # ( 80 | .DSIZE (DSIZE), 81 | .OFFSET(OFFSET) 82 | ) sort_inst1 ( 83 | .a(sortx4_0_1), 84 | .b(sortx4_1_2), 85 | .sort0(sort1_0), 86 | .sort1(sort1_1) 87 | ); 88 | SortElement # ( 89 | .DSIZE (DSIZE), 90 | .OFFSET(OFFSET) 91 | ) sort_inst2 ( 92 | .a(sortx4_0_2), 93 | .b(sortx4_1_1), 94 | .sort0(sort2_0), 95 | .sort1(sort2_1) 96 | ); 97 | SortElement # ( 98 | .DSIZE (DSIZE), 99 | .OFFSET(OFFSET) 100 | ) sort_inst3 ( 101 | .a(sortx4_0_3), 102 | .b(sortx4_1_0), 103 | .sort0(sort3_0), 104 | .sort1(sort3_1) 105 | ); 106 | 107 | // bitonic 108 | BitonicSortX4 # ( 109 | .DSIZE (DSIZE), 110 | .OFFSET(OFFSET) 111 | ) bitonicsortx4_inst0 ( 112 | .a0 (sort0_0), 113 | .a1 (sort1_0), 114 | .a2 (sort2_0), 115 | .a3 (sort3_0), 116 | .sort0 (sort0), 117 | .sort1 (sort1), 118 | .sort2 (sort2), 119 | .sort3 (sort3) 120 | ); 121 | BitonicSortX4 # ( 122 | .DSIZE (DSIZE), 123 | .OFFSET(OFFSET) 124 | ) bitonicsortx4_inst1 ( 125 | .a0 (sort3_1), 126 | .a1 (sort2_1), 127 | .a2 (sort1_1), 128 | .a3 (sort0_1), 129 | .sort0 (sort4), 130 | .sort1 (sort5), 131 | .sort2 (sort6), 132 | .sort3 (sort7) 133 | ); 134 | 135 | endmodule 136 | -------------------------------------------------------------------------------- /simulation/huffman_test.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 2017/07/27 17:45:20 7 | // Design Name: 8 | // Module Name: huffmancode 9 | // Project Name: 10 | // Target Devices: 11 | // Tool Versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | module huffmancode_test(); 23 | reg clk; 24 | reg rst_n; 25 | reg start; // pulse signal 26 | reg start_done; // along with the last data_in, pulse signal 27 | reg [3:0] data_in; 28 | 29 | initial begin 30 | rst_n = 1'b1; 31 | #10 rst_n = 1'b0; 32 | #10 rst_n = 1'b1; 33 | end 34 | initial begin 35 | clk = 1'b1; 36 | forever #1 clk = ~clk; 37 | end 38 | initial begin 39 | start = 1'b0; 40 | #50 start = 1'b1; 41 | #2 start = 1'b0; 42 | end 43 | initial begin 44 | data_in = 4'b0; 45 | #52 data_in = 4'd0; // 5 46 | #10 data_in = 4'd1; // 1 47 | #2 data_in = 4'd2; // 3 48 | #6 data_in = 4'd3; // 2 49 | #4 data_in = 4'd4; // 9 50 | #18 data_in = 4'd5; // 7 51 | #14 data_in = 4'd6; // 4 52 | #8 data_in = 4'd7; // 8 53 | #16 data_in = 4'd8; // 6 54 | #12 data_in = 4'd0; // 0 55 | end 56 | initial begin 57 | start_done = 1'b0; 58 | #140 start_done = 1'b1; 59 | #2 start_done = 1'b0; 60 | end 61 | 62 | wire output_data; // MSB first 63 | wire output_start; 64 | wire output_done; 65 | 66 | huffman_top sim_top( 67 | .clk (clk), 68 | .rst_n (rst_n), 69 | .start (start), // pulse signal 70 | .start_done (start_done), // along with the last data_in, pulse signal 71 | .data_in (data_in), 72 | .output_data (output_data), // MSB first 73 | .output_start (output_start), 74 | .output_done (output_done) 75 | ); 76 | 77 | endmodule 78 | -------------------------------------------------------------------------------- /simulation/sortx16_sim.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns / 1ps 2 | ////////////////////////////////////////////////////////////////////////////////// 3 | // Company: 4 | // Engineer: 5 | // 6 | // Create Date: 2017/07/25 17:45:20 7 | // Design Name: 8 | // Module Name: sortx16_sim 9 | // Project Name: 10 | // Target Devices: 11 | // Tool Versions: 12 | // Description: 13 | // 14 | // Dependencies: 15 | // 16 | // Revision: 17 | // Revision 0.01 - File Created 18 | // Additional Comments: 19 | // 20 | ////////////////////////////////////////////////////////////////////////////////// 21 | 22 | 23 | module sortx16_sim # ( 24 | parameter DSIZE = 5, 25 | parameter OFFSET = 4 26 | )( 27 | output wire [DSIZE-1:0] sort0, 28 | output wire [DSIZE-1:0] sort1, 29 | output wire [DSIZE-1:0] sort2, 30 | output wire [DSIZE-1:0] sort3, 31 | output wire [DSIZE-1:0] sort4, 32 | output wire [DSIZE-1:0] sort5, 33 | output wire [DSIZE-1:0] sort6, 34 | output wire [DSIZE-1:0] sort7, 35 | output wire [DSIZE-1:0] sort8, 36 | output wire [DSIZE-1:0] sort9, 37 | output wire [DSIZE-1:0] sort10, 38 | output wire [DSIZE-1:0] sort11, 39 | output wire [DSIZE-1:0] sort12, 40 | output wire [DSIZE-1:0] sort13, 41 | output wire [DSIZE-1:0] sort14, 42 | output wire [DSIZE-1:0] sort15 43 | ); 44 | 45 | SortX16 # ( 46 | .DSIZE (DSIZE), 47 | .OFFSET(OFFSET) 48 | ) sortx16_inst( 49 | .a0 (5'd3), 50 | .a1 (5'd9), 51 | .a2 (5'd15), 52 | .a3 (5'd6), 53 | .a4 (5'd12), 54 | .a5 (5'd2), 55 | .a6 (5'd8), 56 | .a7 (5'd14), 57 | .a8 (5'd4), 58 | .a9 (5'd10), 59 | .a10 (5'd0), 60 | .a11 (5'd1), 61 | .a12 (5'd7), 62 | .a13 (5'd23), 63 | .a14 (5'd13), 64 | .a15 (5'd5), 65 | .sort0 (sort0), 66 | .sort1 (sort1), 67 | .sort2 (sort2), 68 | .sort3 (sort3), 69 | .sort4 (sort4), 70 | .sort5 (sort5), 71 | .sort6 (sort6), 72 | .sort7 (sort7), 73 | .sort8 (sort8), 74 | .sort9 (sort9), 75 | .sort10 (sort10), 76 | .sort11 (sort11), 77 | .sort12 (sort12), 78 | .sort13 (sort13), 79 | .sort14 (sort14), 80 | .sort15 (sort15) 81 | ); 82 | 83 | endmodule 84 | -------------------------------------------------------------------------------- /software model/HuffNode.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************* 2 | /* Description: 3 | /* high-level model of huffman coding, written in C. 4 | /* it is a static huffman coding 5 | /* input data: 0 - 9 6 | /* totally 256 times 7 | /* Author: 8 | /* Guozhu Xin 9 | /* Date: 10 | /* 2017/7/8 11 | /* Email contact: 12 | /* spikexin@outlook.com 13 | *********************************************************************/ 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | struct HuffNode{ 19 | int Value; // 0 - 9 20 | int Freq; // appear times 21 | HuffNode* LeftNode; 22 | HuffNode* RightNode; 23 | }; 24 | struct HuffNode node[50]; 25 | 26 | void InitNode() 27 | { 28 | node[0] = {0,5,NULL,NULL}; 29 | node[1] = {1,10,NULL,NULL}; 30 | node[2] = {2,15,NULL,NULL}; 31 | node[3] = {3,20,NULL,NULL}; 32 | node[4] = {4,25,NULL,NULL}; 33 | node[5] = {5,30,NULL,NULL}; 34 | node[6] = {6,35,NULL,NULL}; 35 | node[7] = {7,40,NULL,NULL}; 36 | node[8] = {8,45,NULL,NULL}; 37 | node[9] = {9,31,NULL,NULL}; 38 | } 39 | 40 | 41 | void SwitchNode(HuffNode* a, HuffNode* b) 42 | { 43 | HuffNode temp; 44 | temp = *a; 45 | *a = *b; 46 | *b = temp; 47 | } 48 | 49 | void SortNode(int StartIdx, int len) 50 | { 51 | if(len > 1){ 52 | for(int i = StartIdx+len-1; i > StartIdx; i--){ 53 | for(int j = StartIdx; j < i; j++){ 54 | if(node[j].Freq > node[j+1].Freq){ 55 | SwitchNode(&node[j], &node[j+1]); 56 | } 57 | } 58 | } 59 | } 60 | } 61 | 62 | HuffNode GenTree() 63 | { 64 | int Len = 10; 65 | SortNode(0,Len); 66 | int StartIdx = 0,GenIdx = 10; 67 | while(Len > 1){ 68 | Len --; 69 | StartIdx += 2; 70 | node[GenIdx++] = {-1,node[StartIdx-2].Freq+node[StartIdx-1].Freq, 71 | &node[StartIdx-2],&node[StartIdx-1]}; 72 | SortNode(StartIdx,Len); 73 | } 74 | return node[StartIdx]; 75 | } 76 | 77 | void Traverse(HuffNode* node_ptr, string str) 78 | { 79 | if(node_ptr->Value != -1){ 80 | cout << node_ptr->Value << ": " << str << "\n"; 81 | } 82 | else{ 83 | str = str + '0'; 84 | if(node_ptr->LeftNode){ 85 | Traverse(node_ptr->LeftNode,str); 86 | } 87 | str[str.size()-1] = '1'; 88 | if(node_ptr->RightNode){ 89 | Traverse(node_ptr->RightNode,str); 90 | } 91 | } 92 | } 93 | 94 | int main() 95 | { 96 | HuffNode root; 97 | HuffNode* node_ptr; 98 | string str; 99 | InitNode(); 100 | root = GenTree(); 101 | node_ptr = &root; 102 | Traverse(node_ptr,str); 103 | return 0; 104 | } 105 | --------------------------------------------------------------------------------