├── LICENSE ├── README.md ├── command ├── makefile └── top.sv ├── factory ├── factory_registry │ └── top.sv └── factory_simple │ ├── makefile │ └── top.sv ├── objection ├── makefile └── top.sv ├── observer ├── makefile └── top.sv ├── singleton ├── makefile └── top.sv ├── state ├── FSM_usb_task.sv ├── FSM_use_state_pattern.sv └── makefile ├── strategy ├── makefile └── top.sv └── visitor ├── makefile └── top.sv /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # systemverilog_design_pattern 2 | 3 | The source code of [csdn blog](https://blog.csdn.net/holden_liu/category_10656300.html) articles,introducing the design patterns used in UVM and the simple design patterns examples coded in systemverilog. 4 | 5 | These articles introducing 11 kinds design patterns used in UVM: 6 | 7 | 8 | `make run` in every folder to execute. 9 | 10 | -------------------------------------------------------------------------------- /command/makefile: -------------------------------------------------------------------------------- 1 | run: 2 | vcs -sverilog -full64 -timescale="1ns/1ns" +ntb_random_seed_automatic top.sv -R 3 | 4 | 5 | -------------------------------------------------------------------------------- /command/top.sv: -------------------------------------------------------------------------------- 1 | typedef class Soldier; 2 | interface class Command; 3 | pure virtual function void execute(); 4 | endclass 5 | 6 | class FireCommand implements Command; 7 | Soldier m_soldier; 8 | function new(Soldier soldier); 9 | m_soldier = soldier; 10 | endfunction 11 | virtual function void execute(); 12 | m_soldier.fire(); 13 | endfunction 14 | endclass 15 | 16 | class GoCommand implements Command; 17 | Soldier m_soldier; 18 | function new(Soldier soldier); 19 | m_soldier = soldier; 20 | endfunction 21 | virtual function void execute(); 22 | m_soldier.go(); 23 | endfunction 24 | endclass 25 | 26 | class Captain; 27 | Command m_cmd; 28 | function void setCommand(Command cmd); 29 | m_cmd = cmd; 30 | endfunction 31 | function void invoke(); 32 | m_cmd.execute(); 33 | endfunction 34 | endclass 35 | 36 | class Soldier; 37 | function void fire(); 38 | $display("Soldier: execute command, Start Fire! "); 39 | endfunction 40 | function void go(); 41 | $display("Soldier: execute command, GO! Go! Go! "); 42 | endfunction 43 | endclass 44 | 45 | module test; 46 | initial begin 47 | Soldier soldier = new(); 48 | Captain captain = new(); 49 | 50 | FireCommand firecommand = new(soldier); 51 | GoCommand gocommand = new(soldier); 52 | 53 | captain.setCommand(gocommand); 54 | captain.invoke(); 55 | 56 | captain.setCommand(firecommand); 57 | captain.invoke(); 58 | end 59 | endmodule 60 | -------------------------------------------------------------------------------- /factory/factory_registry/top.sv: -------------------------------------------------------------------------------- 1 | virtual class Object; 2 | endclass 3 | 4 | virtual class ObjectProxy; 5 | pure virtual function Object createObj(); 6 | endclass 7 | 8 | 9 | ObjectProxy factory[ObjectProxy]; 10 | 11 | class objectRegistry #(type T) extends ObjectProxy; 12 | 13 | virtual function Object createObj(); 14 | T obj; 15 | obj = new(); 16 | return obj; 17 | endfunction 18 | 19 | local static objectRegistry#(T) me = get(); 20 | 21 | static function objectRegistry#(T) get(); 22 | if (me == null) begin 23 | me = new(); 24 | factory[me] = me; 25 | end 26 | return me; 27 | endfunction : get 28 | 29 | static function T create(); 30 | T obj_h; 31 | $cast(obj_h, factory[get()].createObj()); 32 | //me.createObj(); 33 | return obj_h; 34 | endfunction : create 35 | endclass 36 | 37 | class A extends Object; 38 | typedef objectRegistry#(A) typeId; 39 | virtual function void display(); 40 | $display("This is class A !"); 41 | endfunction 42 | endclass 43 | 44 | class B extends Object; 45 | typedef objectRegistry#(B) typeId; 46 | virtual function void display(); 47 | $display("This is class B !"); 48 | endfunction 49 | endclass 50 | 51 | class C extends A; 52 | typedef objectRegistry#(C) typeId; 53 | function void display(); 54 | $display("This is class C !"); 55 | endfunction 56 | endclass 57 | 58 | module test; 59 | 60 | initial begin 61 | A a_h; 62 | B b_h; 63 | a_h = A::typeId::create(); 64 | a_h.display(); 65 | b_h = B::typeId::create(); 66 | b_h.display(); 67 | factory[A::typeId::get()] = C::typeId::get(); 68 | a_h = A::typeId::create(); 69 | a_h.display(); 70 | end 71 | endmodule 72 | 73 | -------------------------------------------------------------------------------- /factory/factory_simple/makefile: -------------------------------------------------------------------------------- 1 | run: 2 | vcs -sverilog -full64 -timescale="1ns/1ns" +ntb_random_seed_automatic top.sv -R 3 | 4 | 5 | -------------------------------------------------------------------------------- /factory/factory_simple/top.sv: -------------------------------------------------------------------------------- 1 | virtual class Object; 2 | endclass 3 | 4 | class A extends Object; 5 | function void display(); 6 | $display("This is class A!"); 7 | endfunction 8 | endclass 9 | 10 | class B extends Object; 11 | function void display(); 12 | $display("This is class B !"); 13 | endfunction 14 | endclass 15 | 16 | class Factory; 17 | 18 | static function Object create(string name); 19 | A a_h; 20 | B b_h; 21 | 22 | case (name) 23 | "A" : begin a_h= new(); 24 | return a_h; 25 | end 26 | "B" : begin b_h = new(); 27 | return b_h; 28 | end 29 | default: $display ("No such type class "); 30 | endcase 31 | endfunction 32 | endclass 33 | 34 | module top; 35 | initial begin 36 | A a_h; 37 | B b_h; 38 | $cast(a_h,Factory::create("A")); 39 | $cast(b_h,Factory::create("B")); 40 | a_h.display(); 41 | b_h.display(); 42 | $cast(b_h,Factory::create("C")); 43 | end 44 | endmodule 45 | 46 | 47 | -------------------------------------------------------------------------------- /objection/makefile: -------------------------------------------------------------------------------- 1 | run: 2 | vcs -sverilog -full64 -timescale="1ns/1ns" +ntb_random_seed_automatic top.sv -R 3 | 4 | 5 | -------------------------------------------------------------------------------- /objection/top.sv: -------------------------------------------------------------------------------- 1 | class comp; 2 | static int count = -1; 3 | 4 | task do_driver(); 5 | repeat(20) 6 | #1; 7 | $display("do_driver exit time:%0t",$time); 8 | endtask 9 | 10 | task do_monitor(); 11 | while(1) 12 | #1; 13 | $display("do_monitro exit time:%0t",$time); 14 | endtask 15 | 16 | static function void raise_objection(); 17 | count++; 18 | $display("raise_objection time:%0t count:%0d",$time,count); 19 | endfunction 20 | 21 | static function void drop_objection(); 22 | count--; 23 | $display("drop_objection time:%0t count:%0d",$time,count); 24 | endfunction 25 | endclass 26 | 27 | module top; 28 | 29 | comp driver; 30 | comp monitor; 31 | process pro1; 32 | process pro2; 33 | 34 | initial begin 35 | int count; 36 | driver = new(); 37 | monitor = new(); 38 | 39 | fork 40 | begin : driver_main_phase 41 | pro1 = process::self(); 42 | //#5; 43 | comp::raise_objection(); 44 | driver.do_driver(); 45 | comp::drop_objection(); 46 | end 47 | begin : monitor_main_phase 48 | pro2 = process::self(); 49 | //comp::raise_objection(); 50 | monitor.do_monitor(); 51 | //comp::drop_objection(); 52 | end 53 | join_none 54 | begin : process_control 55 | fork 56 | begin 57 | wait(comp::count == -1) begin 58 | pro1.kill(); 59 | pro2.kill(); 60 | end 61 | end 62 | join_any 63 | $display("time:%0t enter next phase ",$time); 64 | end 65 | end 66 | endmodule 67 | -------------------------------------------------------------------------------- /observer/makefile: -------------------------------------------------------------------------------- 1 | run: 2 | vcs -sverilog -full64 -timescale="1ns/1ns" +ntb_random_seed_automatic top.sv -R 3 | 4 | 5 | -------------------------------------------------------------------------------- /observer/top.sv: -------------------------------------------------------------------------------- 1 | 2 | virtual class Observer; 3 | protected string m_name; 4 | pure virtual function void update(string message); 5 | pure virtual function string getname(); 6 | endclass 7 | 8 | class Investor extends Observer; 9 | function new(string name); 10 | m_name = name; 11 | endfunction 12 | 13 | virtual function void update(string message); 14 | $display ("%4s receive message: \"%s\" ", m_name, message); 15 | endfunction 16 | 17 | virtual function string getname(); 18 | return m_name; 19 | endfunction 20 | endclass 21 | 22 | virtual class Subject; 23 | pure virtual function void addObserver(Observer observer); 24 | pure virtual function void deleteObserver(Observer observer); 25 | pure virtual function void notifyObserver(string message); 26 | pure virtual function void change(int price); 27 | endclass 28 | 29 | class Stock extends Subject; 30 | 31 | protected Observer m_observer_hash[string]; 32 | 33 | virtual function void change(int price); 34 | if (price > 20) 35 | notifyObserver("The stock price > 20 Yuan !"); 36 | else if (price < 10) 37 | notifyObserver("The stock price < 10 Yuan !"); 38 | endfunction 39 | 40 | virtual function void addObserver(Observer observer); 41 | m_observer_hash[observer.getname()] = observer; 42 | endfunction 43 | 44 | virtual function void deleteObserver(Observer observer); 45 | m_observer_hash.delete(observer.getname()); 46 | endfunction 47 | 48 | virtual function void notifyObserver(string message); 49 | foreach(m_observer_hash[i]) 50 | m_observer_hash[i].update(message); 51 | endfunction 52 | endclass 53 | 54 | module test; 55 | initial begin 56 | Investor investor1 = new("Jack"); 57 | Investor investor2 = new("Tom"); 58 | 59 | Stock stock = new(); 60 | 61 | stock.addObserver(investor1); 62 | stock.addObserver(investor2); 63 | 64 | stock.change(21); 65 | stock.change(9) ; 66 | end 67 | endmodule 68 | -------------------------------------------------------------------------------- /singleton/makefile: -------------------------------------------------------------------------------- 1 | run: 2 | vcs -sverilog -full64 -timescale="1ns/1ns" +ntb_random_seed_automatic top.sv -R 3 | 4 | 5 | -------------------------------------------------------------------------------- /singleton/top.sv: -------------------------------------------------------------------------------- 1 | package pkg; 2 | 3 | class logger; 4 | local static logger log_h; 5 | int file_h; 6 | 7 | local function new(); 8 | file_h= $fopen ("log.txt","w"); 9 | endfunction 10 | 11 | static function logger get_log_h(); 12 | if (log_h== null) begin 13 | log_h= new(); 14 | $display ("create log_h!"); 15 | end 16 | return log_h; 17 | endfunction 18 | 19 | function write (string str); 20 | $fdisplay (file_h, "write : %s",str); 21 | endfunction 22 | endclass 23 | 24 | class userA; 25 | logger log_h; 26 | function new (); 27 | log_h = logger::get_log_h(); 28 | endfunction 29 | endclass 30 | 31 | class userB; 32 | logger log_h; 33 | function new (); 34 | log_h= logger::get_log_h(); 35 | endfunction 36 | endclass 37 | 38 | endpackage 39 | 40 | module top; 41 | import pkg::*; 42 | 43 | userA a_h; 44 | userB b_h; 45 | 46 | initial begin 47 | a_h = new(); 48 | b_h = new(); 49 | fork 50 | a_h.log_h.write("AAA"); 51 | b_h.log_h.write("BBB"); 52 | join 53 | #1; 54 | $finish(); 55 | end 56 | endmodule 57 | -------------------------------------------------------------------------------- /state/FSM_usb_task.sv: -------------------------------------------------------------------------------- 1 | package PKG; 2 | import uvm_pkg::*; 3 | `include "uvm_macros.svh" 4 | 5 | typedef class state_machine; 6 | `define INTERVALTIME #1ns 7 | class client; 8 | 9 | state_machine FSM; 10 | uvm_event_pool events_pool; 11 | uvm_event to_idle,to_state_a,to_state_b,to_state_c; 12 | 13 | function new(); 14 | events_pool = uvm_event_pool::get_global_pool(); 15 | to_idle = events_pool.get("to_idle"); 16 | to_state_a = events_pool.get("to_state_a"); 17 | to_state_b = events_pool.get("to_state_b"); 18 | to_state_c = events_pool.get("to_state_c"); 19 | endfunction 20 | 21 | task rand_simulate(); 22 | for (int i=0;i<2;i++) begin 23 | bit FLAG = 0; 24 | randsequence (stream) 25 | 26 | stream : first second third last; 27 | first : state_a; 28 | second : state_b {FLAG = 1;} | state_c; 29 | third : if (FLAG ==1) state_c else state_b; 30 | last : state_idle; 31 | 32 | state_idle: {`INTERVALTIME;to_idle.trigger();}; 33 | state_a : {`INTERVALTIME;to_state_a.trigger();}; 34 | state_b : {`INTERVALTIME;to_state_b.trigger();}; 35 | state_c : {`INTERVALTIME;to_state_c.trigger();}; 36 | 37 | endsequence 38 | end 39 | endtask 40 | 41 | task run_fsm(); 42 | state_machine fsm_inst; 43 | fsm_inst = new(); 44 | fork 45 | rand_simulate(); 46 | fsm_inst.start(); 47 | join_none 48 | endtask 49 | endclass 50 | 51 | class state_machine; 52 | 53 | typedef enum { 54 | IDLE,STATE_A,STATE_B,STATE_C 55 | } state_t; 56 | 57 | uvm_event_pool events_pool; 58 | uvm_event to_idle,to_state_a,to_state_b,to_state_c; 59 | 60 | local state_t cur_state; 61 | 62 | extern function new(); 63 | extern function void start(); 64 | extern function void request_state_change(state_t cur_state); 65 | extern task do_idle(); 66 | extern task do_state_a(); 67 | extern task do_state_b(); 68 | extern task do_state_c(); 69 | 70 | endclass 71 | 72 | function state_machine::new(); 73 | events_pool = uvm_event_pool::get_global_pool(); 74 | to_idle = events_pool.get("to_idle"); 75 | to_state_a = events_pool.get("to_state_a"); 76 | to_state_b = events_pool.get("to_state_b"); 77 | to_state_c = events_pool.get("to_state_c"); 78 | endfunction 79 | 80 | function void state_machine::start(); 81 | cur_state = IDLE; 82 | request_state_change(cur_state); 83 | endfunction 84 | 85 | function void state_machine::request_state_change(state_t cur_state); 86 | case(cur_state) 87 | IDLE:begin 88 | fork 89 | begin 90 | $display("Enter %s state!",cur_state.name()); 91 | do_idle(); 92 | end 93 | join_none 94 | return; 95 | end 96 | STATE_A:begin 97 | fork 98 | begin 99 | $display("Enter %s state!",cur_state.name()); 100 | do_state_a(); 101 | end 102 | join_none 103 | return; 104 | end 105 | STATE_B:begin 106 | fork 107 | begin 108 | $display("Enter %s state!",cur_state.name()); 109 | do_state_b(); 110 | end 111 | join_none 112 | return; 113 | end 114 | STATE_C:begin 115 | fork 116 | begin 117 | $display("Enter %s state!",cur_state.name()); 118 | do_state_c(); 119 | end 120 | join_none 121 | return; 122 | end 123 | default : begin 124 | $display("Enter unknow state!"); 125 | $finish; 126 | end 127 | endcase 128 | endfunction 129 | 130 | task state_machine::do_idle(); 131 | state_t cur_state; 132 | $display("IDLE : nothing to do!\n"); 133 | fork: disable_fork 134 | begin 135 | to_state_a.wait_trigger(); 136 | //$display("do something!\n"); 137 | cur_state = STATE_A; 138 | end 139 | join_any 140 | request_state_change(cur_state); 141 | endtask 142 | 143 | task state_machine::do_state_a(); 144 | state_t cur_state; 145 | $display("STATE_A : do something!\n"); 146 | fork: disable_fork 147 | begin 148 | to_state_b.wait_trigger(); 149 | //$display("do something!\n"); 150 | cur_state = STATE_B; 151 | end 152 | begin 153 | to_state_c.wait_trigger(); 154 | //$display("do something!\n"); 155 | cur_state = STATE_C; 156 | end 157 | join_any 158 | disable fork; 159 | request_state_change(cur_state); 160 | endtask 161 | 162 | task state_machine::do_state_b(); 163 | state_t cur_state; 164 | $display("STATE_B : do something!\n"); 165 | fork: disable_fork 166 | begin 167 | to_state_c.wait_trigger(); 168 | //$display("do something!\n"); 169 | cur_state = STATE_C; 170 | end 171 | begin 172 | to_idle.wait_trigger(); 173 | //$display("do something!\n"); 174 | cur_state = IDLE; 175 | end 176 | join_any 177 | disable fork; 178 | request_state_change(cur_state); 179 | endtask 180 | 181 | task state_machine::do_state_c(); 182 | state_t cur_state; 183 | $display("STATE_C : do something!\n"); 184 | fork: disable_fork 185 | begin 186 | to_state_b.wait_trigger(); 187 | //$display("do something!\n"); 188 | cur_state = STATE_B; 189 | end 190 | begin 191 | to_idle.wait_trigger(); 192 | //$display("do something!\n"); 193 | cur_state = IDLE; 194 | end 195 | join_any 196 | disable fork; 197 | request_state_change(cur_state); 198 | endtask 199 | 200 | endpackage : PKG 201 | 202 | module top; 203 | 204 | import PKG::*; 205 | 206 | initial begin 207 | client client_inst; 208 | client_inst = new(); 209 | client_inst.run_fsm(); 210 | end 211 | endmodule : top 212 | -------------------------------------------------------------------------------- /state/FSM_use_state_pattern.sv: -------------------------------------------------------------------------------- 1 | package PKG; 2 | import uvm_pkg::*; 3 | `include "uvm_macros.svh" 4 | 5 | `define INTERVALTIME #1ns 6 | 7 | `define REGISTER_STATE(state,name) \ 8 | begin \ 9 | state_``name fsm_``name; \ 10 | fsm_``name = new(); \ 11 | state_pool[STATE_``state] = fsm_``name; \ 12 | fsm_``name.FSM = this; \ 13 | end 14 | 15 | typedef enum { 16 | STATE_IDLE,STATE_A,STATE_B,STATE_C 17 | } state_t; 18 | 19 | typedef state_machine; 20 | 21 | class client; 22 | 23 | state_machine FSM; 24 | uvm_event_pool events_pool; 25 | uvm_event to_idle,to_state_a,to_state_b,to_state_c; 26 | 27 | function new(); 28 | events_pool = uvm_event_pool::get_global_pool(); 29 | to_idle = events_pool.get("to_idle"); 30 | to_state_a = events_pool.get("to_state_a"); 31 | to_state_b = events_pool.get("to_state_b"); 32 | to_state_c = events_pool.get("to_state_c"); 33 | endfunction 34 | 35 | task rand_simulate(); 36 | for (int i=0;i<2;i++) begin 37 | bit FLAG = 0; 38 | randsequence (stream) 39 | 40 | stream : first second third last; 41 | first : state_a; 42 | second : state_b {FLAG = 1;} | state_c; 43 | third : if (FLAG ==1) state_c else state_b; 44 | last : state_idle; 45 | 46 | state_idle: {`INTERVALTIME;to_idle.trigger();}; 47 | state_a : {`INTERVALTIME;to_state_a.trigger();}; 48 | state_b : {`INTERVALTIME;to_state_b.trigger();}; 49 | state_c : {`INTERVALTIME;to_state_c.trigger();}; 50 | 51 | endsequence 52 | end 53 | endtask 54 | 55 | task run_fsm(); 56 | state_machine fsm_inst; 57 | fsm_inst = new(); 58 | fork 59 | rand_simulate(); 60 | fsm_inst.run(); 61 | join_none 62 | endtask 63 | endclass 64 | 65 | virtual class state; 66 | 67 | state_machine FSM; 68 | uvm_event_pool events_pool; 69 | uvm_event to_idle,to_state_a,to_state_b,to_state_c; 70 | 71 | function new(); 72 | events_pool = uvm_event_pool::get_global_pool(); 73 | to_idle = events_pool.get("to_idle"); 74 | to_state_a = events_pool.get("to_state_a"); 75 | to_state_b = events_pool.get("to_state_b"); 76 | to_state_c = events_pool.get("to_state_c"); 77 | endfunction 78 | pure virtual task do_something(); 79 | pure virtual task request_state_change(); 80 | endclass 81 | 82 | class state_idle extends state; 83 | 84 | task do_something(); 85 | $display("STATE_IDLE : nothing to do!\n"); 86 | endtask 87 | 88 | task request_state_change(); 89 | state_t cur_state; 90 | fork: disable_fork 91 | begin 92 | to_state_a.wait_trigger(); 93 | //$display("do something!\n"); 94 | cur_state = STATE_A; 95 | end 96 | join_any 97 | FSM.set_state(cur_state); 98 | endtask 99 | endclass 100 | 101 | class state_a extends state; 102 | 103 | task do_something(); 104 | $display("STATE_A : do something!\n"); 105 | endtask 106 | 107 | task request_state_change(); 108 | state_t cur_state; 109 | fork: disable_fork 110 | begin 111 | to_state_b.wait_trigger(); 112 | //$display("do something!\n"); 113 | cur_state = STATE_B; 114 | end 115 | begin 116 | to_state_c.wait_trigger(); 117 | //$display("do something!\n"); 118 | cur_state = STATE_C; 119 | end 120 | join_any 121 | disable fork; 122 | FSM.set_state(cur_state); 123 | endtask 124 | endclass 125 | 126 | class state_b extends state; 127 | 128 | task do_something(); 129 | $display("STATE_B : do something!\n"); 130 | endtask 131 | 132 | task request_state_change(); 133 | state_t cur_state; 134 | fork: disable_fork 135 | begin 136 | to_state_c.wait_trigger(); 137 | //$display("do something!\n"); 138 | cur_state = STATE_C; 139 | end 140 | begin 141 | to_idle.wait_trigger(); 142 | //$display("do something!\n"); 143 | cur_state = STATE_IDLE; 144 | end 145 | join_any 146 | disable fork; 147 | FSM.set_state(cur_state); 148 | endtask 149 | endclass 150 | 151 | class state_c extends state; 152 | 153 | task do_something(); 154 | $display("STATE_C : do something!\n"); 155 | endtask 156 | 157 | task request_state_change(); 158 | state_t cur_state; 159 | fork: disable_fork 160 | begin 161 | to_state_b.wait_trigger(); 162 | //$display("do something!\n"); 163 | cur_state = STATE_B; 164 | end 165 | begin 166 | to_idle.wait_trigger(); 167 | //$display("do something!\n"); 168 | cur_state = STATE_IDLE; 169 | end 170 | join_any 171 | disable fork; 172 | FSM.set_state(cur_state); 173 | endtask 174 | endclass 175 | 176 | class state_machine; 177 | 178 | local state state_m; 179 | state state_pool[state_t]; 180 | 181 | function void set_state(state_t state); 182 | $display("Enter %s state!",state.name()); 183 | state_m = state_pool[state]; 184 | endfunction 185 | 186 | 187 | function void fsm_init(); 188 | `REGISTER_STATE(IDLE,idle) 189 | `REGISTER_STATE(A,a) 190 | `REGISTER_STATE(B,b) 191 | `REGISTER_STATE(C,c) 192 | this.set_state(STATE_IDLE); 193 | endfunction 194 | 195 | task run(); 196 | fsm_init(); 197 | forever begin 198 | state_m.do_something(); 199 | state_m.request_state_change(); 200 | end 201 | endtask 202 | endclass 203 | 204 | 205 | endpackage : PKG 206 | 207 | module top; 208 | 209 | import PKG::*; 210 | 211 | initial begin 212 | client client_inst; 213 | client_inst = new(); 214 | client_inst.run_fsm(); 215 | end 216 | endmodule : top 217 | -------------------------------------------------------------------------------- /state/makefile: -------------------------------------------------------------------------------- 1 | run: 2 | vcs -kdb -lca -sverilog -full64 \ 3 | -debug_access+all \ 4 | -ntb_opts uvm \ 5 | -timescale="1ns/1ns" \ 6 | +ntb_random_seed_automatic \ 7 | FSM_use_state_pattern.sv -R 8 | -------------------------------------------------------------------------------- /strategy/makefile: -------------------------------------------------------------------------------- 1 | run: 2 | vcs -sverilog -full64 -timescale="1ns/1ns" +ntb_random_seed_automatic top.sv -R 3 | 4 | 5 | -------------------------------------------------------------------------------- /strategy/top.sv: -------------------------------------------------------------------------------- 1 | interface class add_oil_behavior_interface; 2 | pure virtual function void add_oil(); 3 | endclass : add_oil_behavior_interface 4 | 5 | class add_diesel implements add_oil_behavior_interface; 6 | virtual function void add_oil(); 7 | $display("Please add diesel !\n"); 8 | endfunction 9 | endclass 10 | 11 | class add_gasoline implements add_oil_behavior_interface; 12 | virtual function void add_oil(); 13 | $display("Please add gasoline !\n"); 14 | endfunction 15 | endclass 16 | 17 | virtual class car; 18 | protected add_oil_behavior_interface add_oil_behavior; 19 | function void request_add_oil(); 20 | this.add_oil_behavior.add_oil(); 21 | endfunction 22 | function void set_oil_type(add_oil_behavior_interface add_oil_behavior); 23 | this.add_oil_behavior = add_oil_behavior; 24 | endfunction 25 | endclass 26 | 27 | class sedan extends car; 28 | protected add_gasoline add_gasoline_h; 29 | function new(); 30 | $display("This is sedan car!"); 31 | add_gasoline_h = new(); 32 | set_oil_type(add_gasoline_h); 33 | endfunction 34 | endclass 35 | 36 | class truck extends car; 37 | protected add_diesel add_diesel_h; 38 | function new(); 39 | $display("This is truck car!"); 40 | add_diesel_h = new(); 41 | set_oil_type(add_diesel_h); 42 | endfunction 43 | endclass 44 | 45 | module top; 46 | truck truck_h; 47 | sedan sedan_h; 48 | 49 | add_diesel add_diesel_h; 50 | add_gasoline add_gasoline_h; 51 | 52 | initial begin 53 | 54 | add_gasoline_h = new(); 55 | 56 | sedan_h = new(); 57 | sedan_h.request_add_oil(); 58 | 59 | truck_h = new(); 60 | truck_h.request_add_oil(); 61 | 62 | // add gasoline 63 | truck_h.set_oil_type(add_gasoline_h); 64 | truck_h.request_add_oil(); 65 | 66 | // add diesel 67 | // truck_h.set_oil_type(truck_h.add_diesel_h); // protected 68 | //truck_h.request_add_oil(); 69 | end 70 | endmodule : top 71 | -------------------------------------------------------------------------------- /visitor/makefile: -------------------------------------------------------------------------------- 1 | run: 2 | vcs -sverilog -full64 -timescale="1ns/1ns" +ntb_random_seed_automatic top.sv -R 3 | 4 | 5 | -------------------------------------------------------------------------------- /visitor/top.sv: -------------------------------------------------------------------------------- 1 | package pkg; 2 | 3 | typedef Element; 4 | 5 | interface class Visitor; 6 | pure virtual function void visit(Element element); 7 | endclass 8 | 9 | interface class Element; 10 | pure virtual function void accept(Visitor visitor); 11 | pure virtual function string getrole(); 12 | endclass 13 | 14 | class Teacher implements Element; 15 | 16 | static const string role = "teacher"; 17 | local string name; 18 | local int score; 19 | local int papercount; 20 | 21 | function new (string name,int score,int papercount); 22 | this.name = name; 23 | this.score = score; 24 | this.papercount = papercount; 25 | endfunction 26 | 27 | virtual function void accept (Visitor visitor); 28 | visitor.visit(this); 29 | endfunction 30 | 31 | virtual function string getrole(); 32 | return this.role; 33 | endfunction 34 | 35 | virtual function int getname(); 36 | return this.name; 37 | endfunction 38 | 39 | virtual function int getscore(); 40 | return this.score; 41 | endfunction 42 | 43 | virtual function int getpapercount(); 44 | return this.papercount; 45 | endfunction 46 | endclass 47 | 48 | class Student implements Element; 49 | 50 | static const local string role = "student"; 51 | local string name; 52 | local int score; 53 | local int papercount; 54 | 55 | function new (string name,int score,int papercount); 56 | this.name = name; 57 | this.score = score; 58 | this.papercount = papercount; 59 | endfunction 60 | 61 | virtual function void accept (Visitor visitor); 62 | visitor.visit(this); 63 | endfunction 64 | 65 | virtual function string getrole(); 66 | return this.role; 67 | endfunction 68 | 69 | virtual function int getname(); 70 | return this.name; 71 | endfunction 72 | 73 | virtual function int getscore(); 74 | return this.score; 75 | endfunction 76 | 77 | virtual function int getpapercount(); 78 | return this.papercount; 79 | endfunction 80 | endclass 81 | 82 | class GradeSelection implements Visitor; 83 | virtual function void visit(Element element); 84 | case (element.getrole()) 85 | "teacher" : begin 86 | Teacher teacher; 87 | $cast(teacher,element); 88 | if(teacher.getscore() > 85) 89 | $display("Teacher:%0s score:%0d, acquire Award!\n",teacher.getname(),teacher.getscore()); 90 | end 91 | "student" : begin 92 | Student student; 93 | $cast(student,element); 94 | if (student.getscore() > 90) 95 | $display("Student:%0s score:%0d, acquire Award!\n",student.getname(),student.getscore()); 96 | end 97 | default : $display("No such role!"); 98 | endcase 99 | endfunction 100 | endclass 101 | 102 | class ResearcherSelection implements Visitor; 103 | 104 | virtual function void visit(Element element); 105 | case (element.getrole()) 106 | "teacher" : begin 107 | Teacher teacher; 108 | $cast(teacher,element); 109 | if (teacher.getpapercount() >= 2) 110 | $display("Teacher:%0s papercount:%0d, acquire 1W YUAN!\n",teacher.getname(),teacher.getpapercount()); 111 | end 112 | "student" : begin 113 | Student student; 114 | $cast(student,element); 115 | if (student. getpapercount() >= 1) 116 | $display("Student:%0s papercount:%0d, acquire 1W YUAN!\n",student.getname(),student.getpapercount()); 117 | end 118 | default : $display("No such role!"); 119 | endcase 120 | endfunction 121 | endclass 122 | 123 | class Objectstructure; 124 | 125 | Element element_q[$]; 126 | 127 | virtual function void accept(Visitor visitor); 128 | foreach(element_q[i]) begin 129 | Element element = element_q[i]; 130 | element.accept(visitor); 131 | end 132 | endfunction 133 | 134 | virtual function void addElement(Element element); 135 | element_q.push_back(element); 136 | endfunction 137 | endclass 138 | 139 | endpackage : pkg 140 | 141 | module Visitorclient; 142 | 143 | import pkg::*; 144 | 145 | initial begin 146 | Student student1 = new(.name("Jim"),.score(92),.papercount(0)); 147 | Student student2 = new(.name("Ana"),.score(88),.papercount(2)); 148 | Teacher teacher1 = new(.name("May"),.score(86),.papercount(1)); 149 | Teacher teacher2 = new(.name("Lee"),.score(75),.papercount(3)); 150 | 151 | GradeSelection gradeselection = new(); 152 | ResearcherSelection researcherselection= new(); 153 | 154 | Objectstructure structure = new(); 155 | structure.addElement(student1); 156 | structure.addElement(student2); 157 | structure.addElement(teacher1); 158 | structure.addElement(teacher2); 159 | 160 | structure.accept(researcherselection); 161 | structure.accept(gradeselection); 162 | end 163 | endmodule 164 | 165 | --------------------------------------------------------------------------------