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

Subversion Repositories oms8051mini

[/] [oms8051mini/] [trunk/] [verif/] [agents/] [spi/] [st_m25p20a/] [internal_logic.v] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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