OpenCores
URL https://opencores.org/ocsvn/uart2spi/uart2spi/trunk

Subversion Repositories uart2spi

[/] [uart2spi/] [trunk/] [verif/] [models/] [st_m25p20a/] [internal_logic.v] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 dinesha
// Author: Mehdi SEBBANE
2
// May 2002
3
// Verilog model
4
// project: M25P20 25 MHz,
5
// release: 1.4.1
6
 
7
 
8
 
9
// These Verilog HDL models are provided "as is" without warranty
10
// of any kind, included but not limited to, implied warranty
11
// of merchantability and fitness for a particular purpose.
12
 
13
 
14
 
15
 
16
 
17
`timescale 1ns/1ns
18
`ifdef SFLASH_SPDUP
19
`include "parameter_fast.v"
20
`else
21
`include "parameter.v"
22
`endif
23
 
24
module internal_logic (c, d, w, s, hold, data_to_read, q, data_to_write, page_add_index, add_mem, write_op, read_op, be_enable, se_enable, add_pp_enable, pp_enable, read_enable, data_request);
25
   ////////////////////////////////
26
   // declaration of the parameters
27
   ////////////////////////////////
28
   input c;
29
   input d;
30
   input w;
31
   input s;
32
   input hold;
33
   input[(`NB_BIT_DATA - 1):0] data_to_read;
34
 
35
   output q;
36
   reg q;
37
 
38
   output[(`NB_BIT_DATA - 1):0] data_to_write;
39
   reg[(`NB_BIT_DATA - 1):0] data_to_write;
40
 
41
   output[(`LSB_TO_CODE_PAGE - 1):0] page_add_index;    // position to write data_to_write inside the page
42
   reg[(`LSB_TO_CODE_PAGE - 1):0] page_add_index;
43
 
44
   output[(`NB_BIT_ADD_MEM - 1):0] add_mem;
45
   reg[(`NB_BIT_ADD_MEM - 1):0] add_mem;
46
 
47
   output write_op;
48
   reg write_op;
49
 
50
   output read_op;
51
   reg read_op;
52
 
53
   output be_enable;
54
   reg be_enable;
55
 
56
   output se_enable;
57
   reg se_enable;
58
 
59
   output add_pp_enable;
60
   reg add_pp_enable;
61
 
62
   output pp_enable;
63
   reg pp_enable;
64
 
65
   output read_enable;
66
   reg read_enable;
67
 
68
   output data_request;
69
   reg data_request;
70
 
71
 
72
   ///////////////////////////////////////////////
73
   // declaration of internal variables
74
   ///////////////////////////////////////////////
75
   reg only_rdsr;
76
   reg only_res;
77
   reg write_protect;
78
   reg select_ok;
79
   reg raz;
80
   reg byte_ok;
81
   reg wren;
82
   reg wrdi;
83
   reg rdsr;
84
   reg wrsr;
85
   reg read_data;
86
   reg fast_read;
87
   reg pp;
88
   reg se;
89
   reg be;
90
   reg dp;
91
   reg res;
92
   reg q_bis;
93
   reg protect;
94
   reg wr_cycle;
95
   reg hold_cond;
96
   reg inhib_wren;
97
   reg inhib_wrdi;
98
   reg inhib_rdsr;
99
   reg inhib_wrsr;
100
   reg inhib_read;
101
   reg inhib_pp;
102
   reg inhib_se;
103
   reg inhib_be;
104
   reg inhib_dp;
105
   reg inhib_res;
106
   reg reset_wel;
107
   reg wel;
108
   reg wip;
109
   reg c_int;
110
 
111
   reg [2:0]   cpt;
112
   reg [2:0]   bit_index;       // to allow shift inside a byte 
113
   reg [2:0]   bit_res;         // to allow shift inside signature
114
   reg [2:0]   bit_register;    // to allow shift inside status register
115
 
116
   reg [7:0]   data;
117
   reg [7:0]   adress_1;
118
   reg [7:0]   adress_2;
119
   reg [7:0]   adress_3;
120
   reg [7:0]   sr_mask;
121
   reg [7:0]   signature;
122
 
123
   reg [(`NB_BIT_DATA-1):0]   wr_latch;
124
   reg [(`NB_BIT_DATA-1):0]   page_ini;
125
   reg [(`NB_BIT_DATA-1):0]   data_latch;
126
   reg [(`NB_BIT_DATA-1):0]   register_bis;
127
   reg [(`NB_BIT_DATA-1):0]   register_temp;
128
   reg [(`NB_BIT_DATA-1):0]   status_register;
129
 
130
   reg [(`NB_BPI-1):0]              bp;
131
   reg [(`NB_BIT_ADD_MEM-1):0]      adress;
132
   reg [(`BIT_TO_CODE_MEM-1):0]     cut_add;
133
   reg [(`LSB_TO_CODE_PAGE -1) :0]  lsb_adress;
134
 
135
   integer     byte_cpt;
136
   integer     int_add;
137
   integer     i;
138
   integer     count_enable;
139
 
140
   time        t_only_res ;
141
 
142
   initial
143
   begin
144
      ////////////////////////////////////////////
145
      // Initialization of the internal variables
146
      ////////////////////////////////////////////
147
      only_rdsr      = `FALSE;
148
      only_res       = `FALSE;
149
      write_protect  = `FALSE;
150
      select_ok      = `FALSE;
151
      raz            = `FALSE;
152
      byte_ok        = `FALSE;
153
 
154
      cpt         = 0;
155
      byte_cpt    = 0;
156
 
157
      data_to_write  = 8'bxxxxxxxx;
158
      data_latch     = 8'bxxxxxxxx;
159
      data_request <= `FALSE;
160
 
161
      wren        = `FALSE;
162
      wrdi        = `FALSE;
163
      rdsr        = `FALSE;
164
      wrsr        = `FALSE;
165
      read_data   = `FALSE;
166
      fast_read   = `FALSE;
167
      pp          = `FALSE;
168
      se          = `FALSE;
169
      be          = `FALSE;
170
      dp          = `FALSE;
171
      res         = `FALSE;
172
 
173
      q_bis          = 1'bz;
174
 
175
      register_bis   = 8'b00000000;
176
      wr_latch       = 8'b00000000;
177
 
178
      protect     = `FALSE;
179
      wr_cycle    = `FALSE;
180
      hold_cond   = `FALSE;
181
      write_op    = `FALSE;
182
      read_op     = `FALSE;
183
 
184
      inhib_wren  = `FALSE;
185
      inhib_wrdi  = `FALSE;
186
      inhib_rdsr  = `FALSE;
187
      inhib_wrsr  = `FALSE;
188
      inhib_read  = `FALSE;
189
      inhib_pp    = `FALSE;
190
      inhib_se    = `FALSE;
191
      inhib_be    = `FALSE;
192
      inhib_dp    = `FALSE;
193
      inhib_res   = `FALSE;
194
 
195
      add_pp_enable  = `FALSE;
196
      read_enable    = `FALSE;
197
      pp_enable      = `FALSE;
198
      be_enable      = `FALSE;
199
      se_enable      = `FALSE;
200
 
201
 
202
      count_enable   = `FALSE;
203
      data           = 8'b00000000;
204
 
205
      // decode process
206
      bit_index      = 8'b00000000;
207
      bit_res        = 8'b00000000;
208
      bit_register   = 8'b00000000;
209
 
210
      bp          = `NB_BPI'h0 ;
211
 
212
      int_add     = 0;
213
      sr_mask     = 8'b10000000;
214
      page_ini    = 8'b11111111;
215
 
216
      reset_wel   = 1'b0;
217
      wel         = 1'b0;
218
      wip         = 1'b0;
219
 
220
      signature   = `SIGNATURE ;
221
   end // initial
222
 
223
   always @(register_bis) status_register = register_bis ;
224
   //assign status_register = register_bis ; // Continuous Assignment
225
 
226
   //-----------------------------------------------------------
227
   // This process generates the Hold condition when it is valid
228
   always
229
   begin : hold_com
230
      @(hold);
231
      begin
232
      if ((hold == 1'b0) && (s == 1'b0))
233
      begin
234
         if (c == 1'b0)
235
         begin
236
            hold_cond <= `TRUE;
237
            if ($time != 0) $display("NOTE: COMMUNICATION PAUSED");
238
         end
239
         else
240
         begin
241
            @(c or hold);
242
            if (c == 1'b0)
243
            begin
244
               hold_cond <= `TRUE;
245
               if ($time != 0) $display("NOTE: COMMUNICATION PAUSED");
246
            end
247
         end
248
      end
249
      else if (hold == 1'b1)
250
      begin
251
         if (c == 1'b0)
252
         begin
253
            hold_cond <= `FALSE;
254
            if ($time != 0) $display("NOTE: COMMUNICATION (RE)STARTED");
255
         end
256
         else
257
         begin
258
            @(c or hold);
259
            if (c == 1'b0)
260
            begin
261
               hold_cond <= `FALSE;
262
               if ($time != 0) $display("NOTE: COMMUNICATION (RE)STARTED");
263
            end
264
         end
265
      end
266
      end
267
   end
268
 
269
   //----------------------------------------------------------------------
270
   // This process inhibits the internal clock when hold condition is valid
271
   always
272
   begin : horloge
273
      @(c);
274
      begin
275
      if (!hold_cond)
276
      begin
277
         c_int <= c ;
278
      end
279
      else
280
      begin
281
         c_int <= 1'b0 ;
282
      end
283
      end
284
   end
285
 
286
   //---------------------------------------------------------------
287
   // This process inhibits data output when hold condition is valid
288
   always @(posedge hold_cond ) q <= #`THLQZ 1'bz ;
289
 
290
   always @(negedge hold_cond) q <= #`THHQX q_bis ;
291
 
292
   always @ (q_bis or s)
293
      if (!hold_cond)
294
      begin
295
         q <= q_bis ;
296
      end
297
 
298
   //----------------------------------------------------------
299
   // This process increments 2 counters:  one bit counter (cpt)
300
   // one byte counter (byte_cpt)
301
   always
302
   begin : count_bit_raz
303
         @(raz);
304
         begin
305
            if (raz || !select_ok)
306
            begin
307
               cpt <= 0 ;
308
               byte_cpt <= 0 ;
309
               count_enable = `FALSE;
310
            end
311
         end
312
   end
313
 
314
   always
315
   begin : count_bit_enable
316
      @(posedge c_int or negedge raz);
317
      begin
318
            if (!raz && select_ok)
319
            begin
320
               // count enable is an intermediate variable which allows cpt to be
321
               // constant during a whole period
322
               count_enable = `TRUE;
323
            end
324
      end
325
   end
326
 
327
 
328
   always
329
   begin : count_bit
330
      @(negedge c_int);
331
      begin
332
         if (!raz && select_ok)
333
         begin
334
            if (count_enable)
335
            begin
336
               cpt <= cpt + 1 ;
337
            end
338
         end
339
      end
340
   end
341
 
342
   always @(negedge c_int)
343
      if (byte_ok)
344
      begin
345
         //byte_cpt = (byte_cpt + 1) ; 
346
         byte_cpt <= (byte_cpt + 1) ;
347
      end
348
 
349
   //---------------------------------------------------------------------
350
   // This process latches every byte of data received and returns byte_ok 
351
   always
352
   begin : data_in_reset
353
 
354
      @(c_int or select_ok);
355
      begin
356
         if (!select_ok)
357
         begin
358
            raz <= `TRUE ;
359
            byte_ok <= `FALSE ;
360
            data_latch <= 8'b00000000 ;
361
            data = 8'b00000000;
362
         end
363
      end
364
   end
365
 
366
   always
367
   begin : data_in
368
 
369
      @(posedge c_int);
370
      begin
371
      if (select_ok)
372
      begin
373
         raz <= `FALSE ;
374
         if (cpt == 0)
375
         begin
376
            data_latch <= 8'b00000000 ;
377
            byte_ok <= `FALSE ;
378
         end
379
         data[7 - cpt] = d;
380
         if (cpt == 7)
381
         begin
382
            byte_ok <= `TRUE ;
383
            data_latch <= data ;
384
         end
385
         else data_latch <= 8'bxxxxxxxx;
386
      end
387
      end
388
   end
389
 
390
   //-------------------------------------------------------------
391
   //--------------- ASYNCHRONOUS DECODE PROCESS -----------------
392
   //-------------------------------------------------------------
393
   always
394
   begin : decode
395
      //-------------------------
396
      // status register mask ini
397
      //-------------------------
398
      for(i = 0; i <= (`NB_BPI - 1); i = i + 1)
399
      begin
400
         sr_mask[i + 2] = 1'b1;
401
      end
402
 
403
 
404
      @(byte_ok);
405
      if (byte_ok == 1'b1)
406
      begin
407
         //-----------------------------------------------------------
408
         //-- op_code decode
409
         //-----------------------------------------------------------
410
         if (byte_cpt == 0)
411
         begin
412
            if (data_latch == 8'b00000110)
413
            begin
414
               if (only_rdsr)
415
               begin
416
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle");
417
               end
418
               else if (only_res)
419
               begin
420
                  if ($time != 0) $display("ERROR :This Opcode is not decoded during a DEEP POWER DOWN ");
421
               end
422
               else
423
               begin
424
                  wren <= `TRUE ;
425
                  write_op <= `TRUE ;
426
               end
427
            end
428
            else if (data_latch == 8'b00000100)
429
            begin
430
               if (only_rdsr)
431
               begin
432
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle ");
433
               end
434
               else if (only_res)
435
               begin
436
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a DEEP POWER DOWN ");
437
               end
438
               else
439
               begin
440
                  wrdi <= `TRUE ;
441
                  write_op <= `TRUE ;
442
               end
443
            end
444
            else if (data_latch == 8'b00000101)
445
            begin
446
               if (only_res)
447
               begin
448
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a DEEP POWER DOWN ");
449
               end
450
               else
451
               begin
452
                  rdsr <= `TRUE ;
453
               end
454
            end
455
            else if (data_latch == 8'b00000001)
456
            begin
457
               if (only_rdsr)
458
               begin
459
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle ");
460
               end
461
               else if (only_res)
462
               begin
463
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a DEEP POWER DOWN ");
464
               end
465
               else
466
               begin
467
                  wrsr <= `TRUE ;
468
                  write_op <= `TRUE ;
469
               end
470
            end
471
            else if (data_latch == 8'b00000011)
472
            begin
473
               if (only_rdsr)
474
               begin
475
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle ");
476
               end
477
               else if (only_res)
478
               begin
479
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a DEEP POWER DOWN ");
480
               end
481
               else
482
               begin
483
                  read_data <= `TRUE ;
484
                  read_op <= `TRUE ;
485
               end
486
            end
487
            else if (data_latch == 8'b00001011)
488
            begin
489
               if (only_rdsr)
490
               begin
491
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle ");
492
               end
493
               else if (only_res)
494
               begin
495
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a DEEP POWER DOWN ");
496
               end
497
               else
498
               begin
499
                  fast_read <= `TRUE ;
500
               end
501
            end
502
            else if (data_latch == 8'b00000010)
503
            begin
504
               if (only_rdsr)
505
               begin
506
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle ");
507
               end
508
               else if (only_res)
509
               begin
510
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a DEEP POWER DOWN ");
511
               end
512
               else
513
               begin
514
                  pp <= `TRUE ;
515
                  write_op <= `TRUE ;
516
               end
517
            end
518
            else if (data_latch == 8'b11011000)
519
            begin
520
               if (only_rdsr)
521
               begin
522
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle ");
523
               end
524
               else if (only_res)
525
               begin
526
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a DEEP POWER DOWN ");
527
               end
528
               else
529
               begin
530
                  se <= `TRUE ;
531
                  write_op <= `TRUE ;
532
               end
533
            end
534
            else if (data_latch == 8'b11000111)
535
            begin
536
               if (only_rdsr)
537
               begin
538
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle ");
539
               end
540
               else if (only_res)
541
               begin
542
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a DEEP POWER DOWN ");
543
               end
544
               else
545
               begin
546
                  be <= `TRUE ;
547
                  write_op <= `TRUE ;
548
                  for(i = 0; i <= (`NB_BPI - 1); i = i + 1)
549
                  begin
550
                     bp[i] = status_register[i + 2];
551
                  end
552
                  if (bp != 2'b00)
553
                  begin
554
                     protect <= `TRUE ;
555
                     write_op <= `FALSE ;
556
                  end
557
               end
558
            end
559
            else if (data_latch == 8'b10111001)
560
            begin
561
               if (only_rdsr)
562
               begin
563
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle ");
564
               end
565
               else if (only_res)
566
               begin
567
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a DEEP POWER DOWN ");
568
               end
569
               else
570
               begin
571
                  dp <= `TRUE ;
572
               end
573
            end
574
            else if (data_latch == 8'b10101011)
575
            begin
576
               if (only_rdsr)
577
               begin
578
                  if ($time != 0) $display("ERROR : This Opcode is not decoded during a Prog. Cycle ");
579
               end
580
               else
581
               begin
582
                  res <= `TRUE ;
583
               end
584
            end
585
            else
586
            begin
587
               if ($time != 0) $display("ERROR : False instruction, please retry ");
588
            end
589
         end
590
         //---------------------------------------------------------------------
591
         // addresses and data reception and treatment
592
         //---------------------------------------------------------------------
593
         if ( (byte_cpt == 1) && (!only_rdsr) && (!only_res))
594
         begin
595
            if (((read_data) || (fast_read) || (se) || (pp)) && (!rdsr))
596
            begin
597
               adress_1 = data_latch;
598
            end
599
            else if (wrsr && (!rdsr))
600
            begin
601
               wr_latch <= data_latch ;
602
            end
603
         end
604
 
605
         if ((byte_cpt == 2) && (!only_rdsr) && (!only_res))
606
         begin
607
            if (((read_data) || (fast_read) || (se) || (pp)) && (!rdsr))
608
            begin
609
               adress_2 = data_latch;
610
            end
611
         end
612
         if ((byte_cpt == 3) && (!only_rdsr) && (!only_res))
613
         begin
614
            if (((read_data) || (fast_read) || (se) || (pp)) && (!rdsr))
615
            begin
616
               adress_3 = data_latch;
617
               for(i = 0; i <= (`NB_BIT_ADD - 1); i = i + 1)
618
               begin
619
                  adress[i] = adress_3[i];
620
                  adress[i + `NB_BIT_ADD] = adress_2[i];
621
                  adress[i + 2 * `NB_BIT_ADD] = adress_1[i];
622
                  add_mem <= adress ;
623
               end
624
               for(i = (`LSB_TO_CODE_PAGE - 1); i >= 0; i = i - 1)
625
               begin
626
                  lsb_adress[i] = adress[i];
627
               end
628
            end
629
            if ((se || pp) && (!rdsr))
630
            begin
631
               //-----------------------------------------
632
               // To ignore don't care MSB of the adress
633
               //----------------------------------------- 
634
               for(i = 0; i <= `BIT_TO_CODE_MEM -1; i = i + 1)
635
               begin
636
                  cut_add[i] = adress[i];
637
               end
638
               int_add = cut_add;
639
               //------------------------------------------------
640
               // Sector protection detection
641
               //------------------------------------------------
642
               for(i = 0; i <= (`NB_BPI - 1); i = i + 1)
643
               begin
644
                  bp[i] = status_register[i + 2];
645
               end
646
               if (bp == 2'b11)
647
               begin
648
                  protect <= `TRUE ;
649
                  write_op <= `FALSE ;
650
               end
651
               else if (bp == 2'b10)
652
               begin
653
                  if (int_add >= ((`TOP_MEM + 1) / 2))
654
                  begin
655
                     protect <= `TRUE ;
656
                     write_op <= `FALSE ;
657
                  end
658
               end
659
               else if (bp == 2'b01)
660
               begin
661
                  if (int_add >= ((`TOP_MEM + 1) * 3 / 4))
662
                  begin
663
                     protect <= `TRUE ;
664
                     write_op <= `FALSE ;
665
                  end
666
               end
667
               else
668
               begin
669
                  protect <= `FALSE ;
670
               end
671
            end
672
         end
673
         //---------------------------------------------------------------------------
674
         // PAGE PROGRAM
675
         // The adress's LSBs necessary to code a whole page are converted to a natural
676
         // and used to fullfill the page buffer p_prog the same way as the memory page
677
         // will be fullfilled.
678
         //--------------------------------------------------------------------------
679
         if ( (byte_cpt >= 4) && (pp) && (!only_rdsr) && (!rdsr))
680
         begin
681
            data_to_write = data_latch ;
682
            page_add_index = (byte_cpt - 1 - (`NB_BIT_ADD_MEM / `NB_BIT_ADD) + lsb_adress);
683
         end
684
         else
685
         begin
686
            data_to_write  = 8'bxxxxxxxx;
687
            page_add_index = 8'bxxxxxxxx;
688
         end
689
 
690
         // to launch adress treatment in memory access
691
         if (( read_data && (byte_cpt == 3)) || (fast_read && (byte_cpt == 4)))
692
         begin
693
            read_enable <= `TRUE ;
694
         end
695
         // to send a request for the data pointed by the adress
696
         if (( read_data && (byte_cpt >= 3)) || ( fast_read && (byte_cpt >= 4)))
697
         begin
698
            data_request <= `TRUE ;
699
         end
700
      end   //(if byte_ok)
701
   end   //(process decode)
702
 
703
   //-----------------------------------------
704
   // adresses initialization and reset
705
   //-----------------------------------------
706
   always @(posedge select_ok)
707
   begin
708
      for(i = 0; i <= (`NB_BIT_ADD - 1); i = i + 1)
709
      begin
710
         adress_1[i] = 1'b0;
711
         adress_2[i] = 1'b0;
712
         adress_3[i] = 1'b0;
713
      end
714
      for(i = 0; i <= (`NB_BIT_ADD_MEM - 1); i = i + 1)
715
      begin
716
         adress[i] = 1'b0;
717
      end
718
      add_mem <= adress ;
719
   end
720
 
721
 
722
   always @(negedge byte_ok)
723
   begin
724
      if ((read_data && (byte_cpt > 3) ) || (fast_read && (byte_cpt > 4) ))
725
      begin
726
         data_request <= `FALSE ;
727
 
728
      end
729
   end
730
 
731
   always @(posedge inhib_read)
732
   begin
733
         read_op <= `FALSE ;
734
         read_data <= `FALSE ;
735
         fast_read <= `FALSE ;
736
         read_enable <= `FALSE ;
737
         data_request <= `FALSE ;
738
 
739
   end
740
   //------------------------------------------------------
741
   // STATUS REGISTER INSTRUCTIONS
742
   //------------------------------------------------------
743
   // WREN/WRDI instructions
744
   //-----------------------
745
   always @(posedge wel)
746
   begin
747
      register_bis[1] <= 1'b1 ;
748
 
749
   end
750
 
751
   always @(posedge inhib_wren)
752
   begin
753
      wren <= `FALSE ;
754
      write_op <= `FALSE ;
755
   end
756
 
757
   always @(posedge inhib_wrdi)
758
   begin
759
      wrdi <= `FALSE ;
760
      write_op <= `FALSE ;
761
   end
762
 
763
   //----------------------
764
   // RESET WEL instruction
765
   //----------------------
766
   always @(posedge reset_wel)
767
   begin
768
      register_bis[1] <= 1'b0 ;
769
   end
770
   //-------------------
771
   // WRSR instructions
772
   //-------------------
773
   always @(posedge wr_cycle)
774
      begin
775
         if ($time != 0) $display("NOTE : Write status register cycle has begun ");
776
         register_bis <= ((register_bis) | (8'b00000011)) ;
777
      end
778
   always @(negedge wr_cycle)
779
      begin
780
         if ($time != 0) $display("NOTE : Write status register cycle is finished");
781
         register_bis <= ((wr_latch) & sr_mask) ;
782
         wrsr <= `FALSE ;
783
      end
784
 
785
   always @(posedge inhib_wrsr) wrsr <= `FALSE ;
786
 
787
   always @(negedge wrsr) wr_latch <= 8'b00000000 ;
788
 
789
   //------
790
   // PROG
791
   //------
792
   always @(wip)
793
   begin
794
      if (wip == 1'b1)
795
      begin
796
         register_bis[0] <= 1'b1 ;
797
      end
798
      else
799
      begin
800
         register_bis[0] <= 1'b0 ;
801
         write_op <= `FALSE ;
802
 
803
      end
804
   end
805
   //------------------
806
   // rdsr instruction
807
   //------------------
808
   always @(posedge inhib_rdsr) rdsr <= `FALSE ;
809
   //----------------------------------------------------------
810
   // BULK/SECTOR ERASE INSTRUCTIONS
811
   //----------------------------------------------------------
812
   always @(posedge inhib_be)
813
   begin
814
      protect <= `FALSE ;
815
      be <= `FALSE ;
816
   end
817
   always @(posedge inhib_se)
818
   begin
819
      protect <= `FALSE ;
820
      se <= `FALSE ;
821
   end
822
   //----------------------------------------------------------
823
   //PAGE PROGRAM INSTRUCTIONS
824
   //----------------------------------------------------------
825
   always @(posedge inhib_pp)
826
   begin
827
      protect <= `FALSE ;
828
      pp <= `FALSE ;
829
   end
830
   //----------------------------------------------------------
831
   // DEEP POWER DOWN
832
   // RELEASE FROM DEEP POWER DOWN AND READ ELECTRONIC SIGNATURE
833
   //-----------------------------------------------------------
834
   always @(posedge inhib_dp) dp <= `FALSE ;
835
 
836
   always @(posedge inhib_res) res <= `FALSE ;
837
 
838
   //--------------------------------------------------------
839
   //--------------- SYNCHRONOUS PROCESS  ----------------
840
   //--------------------------------------------------------
841
   always
842
   @(c_int or select_ok)
843
   begin
844
      wel <= 1'b0 ;
845
      reset_wel <= 1'b0 ;
846
   end
847
 
848
      //-------------------------------------------
849
      // READ_data
850
      //-------------------------------------------
851
   always
852
      @(c_int or select_ok)
853
      begin
854
         if ((!read_data) && (!fast_read))
855
         begin
856
            inhib_read <= `FALSE ;
857
         end
858
         if (((byte_cpt == 0) || (byte_cpt == 1) || (byte_cpt == 2) || ((byte_cpt == 3) && (cpt != 7))) && read_data && (!select_ok))
859
         begin
860
            if ($time != 0) $display("WARNING : Instruction canceled because the chip is deselected");
861
            inhib_read <= `TRUE ;
862
            bit_index <= 8'b00000000;
863
         end
864
         if (read_data && (((byte_cpt == 3) && (cpt == 7)) || (byte_cpt >= 4)))
865
         begin
866
            if (!select_ok)
867
            begin
868
               inhib_read <= `TRUE ;
869
               bit_index <= 8'b00000000;
870
               q_bis <= #`TSHQZ 1'bz ;
871
            end
872
         end
873
      end
874
 
875
   always
876
   @(negedge c_int)
877
   begin
878
      if (read_data && (((byte_cpt == 3) && (cpt == 7)) || (byte_cpt >= 4)))
879
      begin
880
 
881
         if (select_ok)
882
         begin
883
            q_bis <= #`TCLQV data_to_read[7 - bit_index] ;
884
            bit_index = bit_index + 1;
885
         end
886
      end
887
   end
888
 
889
      //------------------------------------------------------------------
890
      // Fast_Read
891
      //------------------------------------------------------------------
892
   always
893
      @(c_int or select_ok)
894
      begin
895
         if (((byte_cpt == 0) || (byte_cpt == 1) || (byte_cpt == 2) || (byte_cpt == 3) || ((byte_cpt == 4) && (cpt != 7))) && fast_read && (!select_ok))
896
         begin
897
            if ($time != 0) $display("WARNING : Instruction canceled because the chip is deselected");
898
            inhib_read <= `TRUE ;
899
            bit_index <= 8'b00000000;
900
         end
901
         if (fast_read && (((byte_cpt == 4) && (cpt == 7)) || (byte_cpt >= 5)))
902
         begin
903
            if (!select_ok)
904
            begin
905
               inhib_read <= `TRUE ;
906
               bit_index <= 8'b00000000;
907
               q_bis <= #`TSHQZ 1'bz ;
908
            end
909
         end
910
      end
911
 
912
   always
913
      @(negedge c_int)
914
      begin
915
         if (fast_read && (((byte_cpt == 4) && (cpt == 7)) || (byte_cpt >= 5)))
916
         begin
917
            if (select_ok)
918
            begin
919
               q_bis <= #`TCLQV data_to_read[7 - bit_index] ;
920
               bit_index = bit_index + 1 ;
921
            end
922
         end
923
      end
924
 
925
      //-----------------------------------------
926
      // Write_enable 
927
      //-----------------------------------------
928
   always
929
      @(c_int or select_ok)
930
      begin
931
         if (!wren)
932
         begin
933
            inhib_wren <= `FALSE ;
934
         end
935
         if (wren && (!only_rdsr) && (!only_res))
936
         begin
937
            if (!select_ok)
938
            begin
939
               wel <= 1'b1 ;
940
               inhib_wren <= `TRUE ;
941
            end
942
         end
943
      end
944
 
945
   always
946
      @(posedge c_int)
947
      begin
948
         if (wren && (!only_rdsr) && (!only_res) && select_ok)
949
         begin
950
            inhib_wren <= `TRUE ;
951
            if ($time != 0) $display("WARNING : Instruction canceled because the chip is still selected");
952
         end
953
      end
954
 
955
      //-------------------------------------------
956
      // Write_disable
957
      //-------------------------------------------
958
   always
959
      @(c_int or select_ok)
960
      begin
961
         if (!wrdi)
962
         begin
963
            inhib_wrdi <= `FALSE ;
964
         end
965
         if (wrdi && (!only_rdsr) && (!only_res))
966
         begin
967
            if (!select_ok)
968
            begin
969
               reset_wel <= 1'b1 ;
970
               inhib_wrdi <= `TRUE ;
971
            end
972
         end
973
      end
974
 
975
   always
976
      @(posedge c_int)
977
      begin
978
         if (wrdi && (!only_rdsr) && (!only_res) && select_ok)
979
         begin
980
            inhib_wrdi <= `TRUE ;
981
            if ($time != 0) $display("WARNING : Instruction canceled because the chip is still selected");
982
         end
983
      end
984
 
985
      //-----------------------------------------
986
      // Write_status_register
987
      //-----------------------------------------
988
   always
989
      @(c_int or select_ok)
990
      begin
991
         if (!wrsr)
992
         begin
993
            inhib_wrsr <= `FALSE ;
994
         end
995
         if (wrsr && (!only_rdsr) && (!only_res))
996
         begin
997
            if ((byte_cpt == 1) && ((cpt != 7) || (!byte_ok)) && (!wr_cycle))
998
            begin
999
               if (!select_ok)
1000
               begin
1001
                  if ($time != 0) $display("WARNING : Instruction canceled because the chip is deselected");
1002
                  inhib_wrsr <= `TRUE ;
1003
               end
1004
            end
1005
            else if ((byte_cpt == 1) && (cpt == 7) && byte_ok)
1006
            begin
1007
               if (write_protect)
1008
               begin
1009
                  if ($time != 0) $display("WARNING : Instruction canceled because status register is hardware protected");
1010
                  inhib_wrsr <= `TRUE ;
1011
               end
1012
            end
1013
         end
1014
      end
1015
 
1016
   always
1017
      @(negedge select_ok)
1018
      begin
1019
         if (wrsr && (!only_rdsr) && (!only_res))
1020
         begin
1021
            if ((byte_cpt == 1) && (cpt == 7) && byte_ok && (!write_protect))
1022
            begin
1023
               if ((status_register[1]) == 1'b0)
1024
               begin
1025
                  if ($time != 0) $display("WARNING : Instruction canceled because WEL is reset");
1026
                  inhib_wrsr <= `TRUE ;
1027
               end
1028
               else
1029
               begin
1030
                  wr_cycle <= `TRUE ;
1031
                  wip <= 1'b1 ;
1032
                  #`TW;
1033
                  wip <= 1'b0 ;
1034
                  wr_cycle <= `FALSE ;
1035
               end
1036
            end
1037
            else if (byte_cpt == 2)
1038
            begin
1039
               if ((status_register[1]) == 1'b0)
1040
               begin
1041
                  if ($time != 0) $display("WARNING : Instruction canceled because WEL is reset");
1042
                  inhib_wrsr <= `TRUE ;
1043
               end
1044
               else
1045
               begin
1046
                  wr_cycle <= `TRUE ;
1047
                  wip <= 1'b1 ;
1048
                  #`TW;
1049
                  wr_cycle <= `FALSE ;
1050
                  wip <= 1'b0 ;
1051
               end
1052
            end
1053
         end
1054
      end
1055
 
1056
   always @(posedge c_int)
1057
   begin
1058
      if (wrsr && (!only_rdsr) && (!only_res))
1059
         if (byte_cpt == 2 && !rdsr)
1060
         begin
1061
            if ($time != 0) $display("WARNING : Instruction canceled because the chip is still selected");
1062
            inhib_wrsr <= `TRUE ;
1063
         end
1064
      end
1065
 
1066
      //-------------------------------------------
1067
      // Bulk_erase
1068
      //-------------------------------------------
1069
   always @( c_int or select_ok)
1070
   begin
1071
      if (!be)
1072
      begin
1073
         inhib_be <= `FALSE ;
1074
      end
1075
      if (be && (!only_rdsr) && (!only_res))
1076
      begin
1077
         if (!select_ok)
1078
         begin
1079
            if ((status_register[1]) == 1'b0)
1080
            begin
1081
               if ($time != 0) $display("WARNING : Instruction canceled because WEL is reset");
1082
               be_enable <= `FALSE ;
1083
               inhib_be <= `TRUE ;
1084
            end
1085
            else if (protect && be)
1086
            begin
1087
               if ($time != 0) $display("WARNING : Instruction canceled because at least one sector is protected");
1088
               be_enable <= `FALSE ;
1089
               inhib_be <= `TRUE ;
1090
            end
1091
            else
1092
            begin
1093
               if ($time != 0) $display("NOTE : Bulk erase cycle has begun");
1094
               be_enable <= `TRUE ;
1095
               wip <= 1'b1 ;
1096
               // We can't use `TBE since it wider than a 32bits number
1097
               // and the delay instruction can only manage 32bits wide number
1098
               #`TSE ;
1099
               #`TSE ;
1100
               if ($time != 0) $display("NOTE : Bulk erase cycle is finished");
1101
               be_enable <= `FALSE ;
1102
               inhib_be <= `TRUE ;
1103
               wip <= 1'b0 ;
1104
               reset_wel <= 1'b1 ;
1105
            end
1106
         end
1107
      end
1108
   end
1109
 
1110
   always @(posedge c_int)
1111
   begin
1112
      if (be && (!only_rdsr) && (!only_res))
1113
      begin
1114
         if ($time != 0) $display("WARNING : Instruction canceled because the chip is still selected");
1115
         inhib_be <= `TRUE ;
1116
      end
1117
   end
1118
 
1119
      //-------------------------------------------
1120
      // Sector_erase
1121
      //-------------------------------------------
1122
   always @( c_int or select_ok)
1123
   begin
1124
      if (!se)
1125
      begin
1126
         inhib_se <= `FALSE ;
1127
      end
1128
      if (((byte_cpt == 0) || (byte_cpt == 1) || (byte_cpt == 2) || ((byte_cpt == 3) && ((cpt != 7) || !byte_ok))) && se && (!only_rdsr) && (!only_res))
1129
      begin
1130
         if (!select_ok)
1131
         begin
1132
            if ($time != 0) $display("WARNING : Instruction canceled because the chip is deselected");
1133
            inhib_se <= `TRUE ;
1134
         end
1135
      end
1136
      if ( ((byte_cpt == 4) || ((byte_cpt == 3) && (cpt == 7) && byte_ok)) && se && (!only_rdsr) && (!only_res))
1137
      begin
1138
         if (!select_ok)
1139
         begin
1140
            if ((status_register[1]) == 1'b0)
1141
            begin
1142
               if ($time != 0) $display("WARNING : Instruction canceled because WEL is reset");
1143
               se_enable <= `FALSE ;
1144
               inhib_se <= `TRUE ;
1145
            end
1146
            else if (protect && se)
1147
            begin
1148
               if ($time != 0) $display("WARNING : Instruction canceled because the SE sector is protected");
1149
               se_enable <= `FALSE ;
1150
               inhib_se <= `TRUE ;
1151
            end
1152
            else
1153
            begin
1154
               if ($time != 0) $display("NOTE : Sector erase cycle has begun");
1155
               se_enable <= `TRUE ;
1156
               wip <= 1'b1 ;
1157
               #`TSE ;
1158
               if ($time != 0) $display("NOTE : Sector erase cycle is finished");
1159
               se_enable <= `FALSE ;
1160
               inhib_se <= `TRUE ;
1161
               wip <= 1'b0 ;
1162
               reset_wel <= 1'b1 ;
1163
            end
1164
         end
1165
      end
1166
   end
1167
 
1168
   always @(posedge c_int)
1169
   begin
1170
      if ( ((byte_cpt == 4) || ((byte_cpt == 3) && (cpt == 7) && byte_ok)) && se && (!only_rdsr) && (!only_res))
1171
      begin
1172
         if (byte_cpt == 4 && select_ok)
1173
         begin
1174
            if ($time != 0) $display("WARNING : Instruction canceled because the chip is still selected");
1175
            inhib_se <= `TRUE ;
1176
         end
1177
      end
1178
   end
1179
 
1180
      //-------------------------------------------
1181
      // Page_Program
1182
      //-------------------------------------------
1183
   always @(c_int or select_ok)
1184
   begin
1185
      if (!pp)
1186
      begin
1187
         inhib_pp <= `FALSE ;
1188
         add_pp_enable <= `FALSE ;
1189
         pp_enable <= `FALSE ;
1190
      end
1191
 
1192
      if (((byte_cpt == 5) || ((byte_cpt == 4) && (cpt == 7))) && pp && (!only_rdsr) && (!only_res))
1193
      begin
1194
         add_pp_enable <= `TRUE ;
1195
         if ((status_register[1]) == 1'b0)
1196
         begin
1197
            if ($time != 0) $display("WARNING : Instruction canceled because WEL is reset");
1198
            pp_enable <= `FALSE ;
1199
            inhib_pp <= `TRUE ;
1200
         end
1201
         else if (protect && pp)
1202
         begin
1203
            if ($time != 0) $display("WARNING : Instruction canceled because the PP sector is protected");
1204
            pp_enable <= `FALSE ;
1205
            inhib_pp <= `TRUE ;
1206
         end
1207
      end
1208
   end
1209
 
1210
   always @(negedge select_ok)
1211
   begin
1212
      if (((byte_cpt == 0) || (byte_cpt == 1) || (byte_cpt == 2) || (byte_cpt == 3) || ((byte_cpt == 4) && ((cpt != 7) || !byte_ok))) && pp && (!only_rdsr) && (!only_res) && (!select_ok))
1213
         begin
1214
            if ($time != 0) $display("WARNING : Instruction canceled because the chip is deselected");
1215
            inhib_pp <= `TRUE ;
1216
         end
1217
      if (((byte_cpt == 5) || ((byte_cpt == 4) && (cpt == 7))) && pp && (!only_rdsr) && (!only_res))
1218
      begin
1219
         add_pp_enable <= `TRUE ;
1220
         if (pp)
1221
         begin
1222
            if ($time != 0) $display("NOTE : Page program cycle is started");
1223
            wip <= 1'b1 ;
1224
            #`TPP;
1225
            if ($time != 0) $display("NOTE : Page program cycle is finished");
1226
            pp_enable <= `TRUE ;
1227
            wip <= 1'b0 ;
1228
            inhib_pp <= `TRUE ;
1229
            reset_wel <= 1'b1 ;
1230
         end
1231
      end
1232
      if ((byte_cpt > 5) && pp && (!only_rdsr) && (!only_res) && byte_ok)
1233
      begin
1234
         if ($time != 0) $display("NOTE : Page program cycle is started");
1235
         wip <= 1'b1 ;
1236
         #`TPP;
1237
         if ($time != 0) $display("NOTE : Page program cycle is finished");
1238
         pp_enable <= `TRUE ;
1239
         wip <= 1'b0 ;
1240
         inhib_pp <= `TRUE ;
1241
         reset_wel <= 1'b1 ;
1242
      end
1243
      if ((byte_cpt > 5) && pp && (!only_rdsr) && (!only_res) && (!byte_ok))
1244
      begin
1245
         if ($time != 0) $display("WARNING : Instruction canceled because the chip is deselected");
1246
         inhib_pp <= `TRUE ;
1247
         pp_enable <= `FALSE ;
1248
      end
1249
   end
1250
 
1251
      //-----------------------------------------
1252
      // Deep Power Down
1253
      //-----------------------------------------
1254
   always @(c_int or select_ok)
1255
   begin
1256
      if (!dp)
1257
      begin
1258
         inhib_dp <= `FALSE ;
1259
         only_res <= `FALSE ;
1260
         t_only_res = 0;
1261
      end
1262
   end
1263
 
1264
   always @(posedge c_int)
1265
   begin
1266
      if (dp && (!only_rdsr) && (!only_res) && (!res))
1267
      begin
1268
         if ($time != 0) $display("WARNING : Instruction canceled because the chip is still selected");
1269
         inhib_dp <= `TRUE ;
1270
         only_res <= `FALSE ;
1271
         t_only_res = 0;
1272
      end
1273
   end
1274
 
1275
   always @(negedge select_ok)
1276
   begin
1277
      if (dp && (!only_rdsr) && (!only_res) && (!res))
1278
      begin
1279
         if ($time != 0) $display("NOTE : Chip is entering deep power down mode");
1280
         // useful when chip is selected again to inhib every op_code except RES
1281
         // and to check tDP
1282
         t_only_res = $time;
1283
         only_res <= `TRUE ;
1284
 
1285
      end
1286
   end
1287
 
1288
      //---------------------------------------------------------------------
1289
      // Release from Deep Power Down Mode and Read Electronic Signature
1290
      //---------------------------------------------------------------------
1291
   always @(select_ok or c_int)
1292
   begin
1293
      if (!res)
1294
      begin
1295
         inhib_res <= `FALSE ;
1296
         bit_res <= 0;
1297
      end
1298
 
1299
      if (res && ((byte_cpt == 1) && (cpt == 0)) && (!only_rdsr) && (!select_ok))
1300
      begin
1301
      if (only_res)
1302
      begin
1303
         if ($time != 0) $display("NOTE : The chip is releasing from DEEP POWER DOWN");
1304
      end
1305
         inhib_res <= `FALSE;
1306
         inhib_dp <= `FALSE;
1307
         #`TRES1;
1308
         inhib_res <= `TRUE;
1309
         inhib_dp <= `TRUE;
1310
      end
1311
      else if ((((byte_cpt == 1) && (cpt > 0)) || (byte_cpt == 2) || (byte_cpt == 3) || ((byte_cpt == 4) && ((cpt < 7) || (!byte_ok)))) && res && (!only_rdsr) && (!select_ok))
1312
      begin
1313
         if ($time != 0) $display("ERROR : Electronic Signature must be read at least once. Instruction not valid");
1314
      end
1315
      else if ((((byte_cpt == 4) && (cpt == 7) && byte_ok) || (byte_cpt > 4)) && res && (!only_rdsr) && (!select_ok))
1316
      begin
1317
         #`TRES2;
1318
         inhib_res <= `TRUE ;
1319
         inhib_dp <=  `TRUE ;
1320
         if (only_res)
1321
         begin
1322
            if ($time != 0) $display("NOTE : The Chip is releasing from DEEP POWER DOWN");
1323
         end
1324
         q_bis <= 1'bz ;
1325
      end
1326
   end
1327
 
1328
   always @(negedge c_int)
1329
   begin
1330
      if ((((byte_cpt == 3) && (cpt == 7)) || (byte_cpt >= 4)) && res && (!only_rdsr) )
1331
      begin
1332
         q_bis <= #`TCLQV signature[7 - bit_res] ;
1333
         bit_res = bit_res + 1;
1334
      end
1335
   end
1336
 
1337
   //-------------------------------------------------------
1338
   // This process shifts out status register on data output
1339
   always
1340
      @(c_int or select_ok or rdsr)
1341
      begin
1342
         if (!rdsr)
1343
         begin
1344
            inhib_rdsr <= `FALSE ;
1345
         end
1346
         if (rdsr && (!select_ok))
1347
         begin
1348
            bit_register <= 0;
1349
            q_bis <= #`TSHQZ 1'bz ;
1350
            inhib_rdsr <= `TRUE ;
1351
         end
1352
      end
1353
 
1354
   always
1355
      @(negedge c_int)
1356
      begin
1357
         if (rdsr && (select_ok))
1358
         begin
1359
            q_bis <= #`TCLQV status_register[7 - bit_register] ;
1360
            bit_register = bit_register + 1;
1361
         end
1362
      end
1363
   //--------------------------------------------------------------------------------------
1364
   // This process checks select and deselect conditions. Some other conditions are tested:
1365
   // prog cycle, deep power down mode and read electronic signature.
1366
   always
1367
   begin : pin_s
1368
      @(s);
1369
      begin
1370
      if (s == 1'b0)
1371
      begin
1372
         if (res && only_res)
1373
         begin
1374
            if ($time != 0) $display("ERROR : The chip must not be selected until tRES");
1375
         end
1376
         else if (dp)
1377
         begin
1378
            if (($time - t_only_res) < `TDP)
1379
            begin
1380
               if ($time != 0) $display("ERROR : The chip must not be selected until tDP");
1381
            end
1382
            else
1383
            begin
1384
               if ($time != 0) $display("NOTE : Only a Read Electronic Signature instruction will be valid");
1385
            end
1386
         end
1387
         select_ok <= `TRUE ;
1388
         if (pp || wrsr || be || se)
1389
         begin
1390
            if ($time != 0) $display("NOTE : Only a Read Status Register instruction will be valid");
1391
            only_rdsr <= `TRUE ;
1392
         end
1393
      end
1394
      else
1395
      begin
1396
         select_ok <= `FALSE ;
1397
         only_rdsr <= `FALSE ;
1398
      end
1399
      end
1400
   end
1401
 
1402
   //--------------------------------------------------------------
1403
   // This Process detects the hardware protection mode
1404
   always
1405
      @(w or c_int)
1406
      begin
1407
         if ((w == 1'b0) && ((status_register[7]) == 1'b1))
1408
         begin
1409
            write_protect <= `TRUE ;
1410
         end
1411
         if (w == 1'b1)
1412
         begin
1413
            write_protect <= `FALSE ;
1414
         end
1415
      end
1416
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.