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

Subversion Repositories scarm

[/] [scarm/] [trunk/] [src/] [scBarrelShifter.cpp] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 zhong
///////////////////////////////////////////////////////////////////////////////
2
// This program is free software; you can redistribute it and/or
3
// modify it under the terms of the GNU General Public License
4
// as published by the Free Software Foundation; either version 2
5
// of the License, or (at your option) any later version.
6
//
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
// GNU General Public License for more details.
11
//
12
// You should have received a copy of the GNU General Public License
13
// along with this program; if not, write to the Free Software
14
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15
//////////////////////////////////////////////////////////////////////
16
 
17
///////////////////////////////////////////////////////////////////              
18
//          
19
//  Original Author: Allen Tao Zhong,
20
//  University of Electronic Science and Technology in China
21
//  email: zhong@opencores.org
22
//  info   This is a SystemC ARM model 
23
//   
24
//
25
///////////////////////////////////////////////////////////////////////////////
26
 
27
/*****************************************************************************
28
 
29
  scBarrelShifter.cpp -- a barrel shifter
30
 
31
  Original Author: Allen Tao Zhong, University of Electronic Science and
32
                   Technology in China
33
 
34
 *****************************************************************************/
35
 
36
// scBarrelShifter.cpp: implementation of the scBarrelShifter class.
37
//
38
//////////////////////////////////////////////////////////////////////
39
 
40
#include "scBarrelShifter.h"
41
 
42
//////////////////////////////////////////////////////////////////////
43
// Construction/Destruction
44
//////////////////////////////////////////////////////////////////////
45
 
46
#define SHIFT_LEFT(_x,_y)  ((_y >= 32) ? 0 : _x << _y)
47
#define SHIFT_RIGHT(_x,_y) ((_y >= 32) ? 0 : _x >> _y)
48
#define SIGNED_SHIFT_RIGHT(_x,_y) ((_y >= 32) ? ((_x >> 31) * 0xFFFFFFFF) : ((int32_t)_x) >> _y)
49
 
50
///////////////////////////////////////////////////////////////////////////////
51
//
52
//
53
uint32_t scBarrelShifter::Shift(uint32_t nVal,SHIFT type, int nDist)
54
{
55
        //out_b_RW=0;
56
    out_CPSR=R_CPSR;
57
        uint32_t nCPSR=inout_n_Flag;
58
  switch (type)
59
    {
60
      ////////////////////////////////////////////////////////////////
61
      case S_LSL: case S_ASL:
62
      {
63
                 if (nDist == 0)
64
                 {
65
                        m_nShiftCarryBit = (nCPSR & C_FLAG) ? 1 : 0;
66
                        return nVal;
67
                 }
68
                 else if (nDist == 32)
69
                 {
70
                        m_nShiftCarryBit = nVal & 0x1;
71
                        return 0;
72
                 }
73
             else if (nDist > 32)
74
                 {
75
                        m_nShiftCarryBit = 0;
76
                        return 0;
77
                 }
78
                 else
79
                 {
80
                        m_nShiftCarryBit = SHIFT_RIGHT(nVal, (32 - nDist)) & 0x1;
81
                        return (nVal << nDist); break;
82
                 }
83
      }
84
      break;
85
      ////////////////////////////////////////////////////////////////
86
    case S_LSR:
87
      {
88
        if (nDist == 0)
89
          {
90
            m_nShiftCarryBit = (nCPSR & C_FLAG) ? 1 : 0;
91
            return nVal;
92
          }
93
        else if (nDist == 32)
94
          {
95
            m_nShiftCarryBit = (nVal >> 31);
96
            return 0;
97
          }
98
        else if (nDist > 32)
99
          {
100
            m_nShiftCarryBit = 0;
101
            return 0;
102
          }
103
        else
104
          {
105
            m_nShiftCarryBit = (nVal >> (nDist - 1)) & 0x1;
106
            return (nVal >> nDist);
107
          }
108
      }
109
      break;
110
      ////////////////////////////////////////////////////////////////
111
    case S_ASR:
112
      {
113
        if (nDist == 0)
114
          {
115
            m_nShiftCarryBit = (nCPSR & C_FLAG) ? 1 : 0;
116
            return nVal;
117
          }
118
        else if (nDist >= 32)
119
          {
120
            if (nVal & 0x80000000)
121
              {
122
                m_nShiftCarryBit = 1;
123
                return 0xFFFFFFFF;
124
              }
125
            else
126
              {
127
                m_nShiftCarryBit = 0;
128
                return 0;
129
              }
130
          }
131
        else
132
          {
133
            uint32_t temp = 0xFFFFFFFF;
134
            m_nShiftCarryBit = (nVal >> (nDist - 1)) & 0x1;
135
            if ((nVal & 0x80000000) != 0)
136
              nVal = (nVal >> nDist) | (~(temp >> (nDist /* - 1 */)));
137
            else
138
              nVal = (nVal >> nDist);
139
 
140
            return nVal;
141
          }
142
      }
143
      break;
144
      ////////////////////////////////////////////////////////////////
145
    case S_ROR:
146
      {
147
        if (nDist == 0)
148
          {
149
            m_nShiftCarryBit = (nCPSR & C_FLAG) ? 1 : 0;
150
            return nVal;
151
          }
152
        else if ((nDist & 0x1F) == 0)
153
          {
154
            m_nShiftCarryBit = nVal >> 31;
155
            return nVal;
156
          }
157
        else
158
          {
159
            nDist &= 0x1F;
160
            m_nShiftCarryBit = SHIFT_RIGHT(nVal, (nDist - 1)) & 0x1;
161
            return (nVal >> nDist) | (nVal << (32 - nDist));
162
          }
163
      }
164
      break;
165
      ////////////////////////////////////////////////////////////////
166
    case S_RRX:
167
      {
168
            m_nShiftCarryBit = nVal & 0x1;
169
 
170
        uint32_t temp = nVal & 0x1;
171
        nVal = nVal >> 1;
172
        if (nCPSR & C_FLAG)
173
          nVal |= 0x80000000;
174
 
175
        return nVal;
176
      }
177
      break;
178
    default :
179
        {
180
          cout<<"error in scBarrelShifter.Shift\n";
181
                  return 0;
182
 
183
        }
184
   }//end of switch(type)
185
        out_b_RW=1;
186
    out_CPSR=R_CPSR;
187
        inout_n_Flag=nCPSR&(m_nShiftCarryBit<<29);
188
}
189
 
190
void scBarrelShifter::entry()
191
{
192
 
193
   uint32_t val,dist,new_val;
194
   val=in_n_Val;
195
   dist=in_n_Dist;
196
   new_val=Shift(val, in_SHIFT_TYPE, dist);
197
   out_n_NewVal=new_val;
198
   out_b_Carry=m_nShiftCarryBit?1:0;
199
 
200
   //cout<<" Shifter: Value="<<hex<<in_n_Val<<"  ShitfDist="<<in_n_Dist<<" shifted_value="<<hex<<new_val<<endl;
201
}
202
 

powered by: WebSVN 2.1.0

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