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

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.7/] [tools/] [src/] [librtools/] [RosPrintBvi.cpp] - Blame information for rev 36

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 wfjm
// $Id: RosPrintBvi.cpp 492 2013-02-24 22:14:47Z mueller $
2 10 wfjm
//
3 19 wfjm
// Copyright 2011-2013 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 10 wfjm
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16 19 wfjm
// 2013-02-03   481   1.0.2  use Rexception
17 10 wfjm
// 2011-03-12   368   1.0.1  allow base=0, will print in hex,oct and bin
18
// 2011-03-05   366   1.0    Initial version
19
// ---------------------------------------------------------------------------
20
 
21
/*!
22
  \file
23 19 wfjm
  \version $Id: RosPrintBvi.cpp 492 2013-02-24 22:14:47Z mueller $
24 10 wfjm
  \brief   Implemenation of RosPrintBvi .
25
*/
26
 
27
#include "RosPrintBvi.hpp"
28
 
29 19 wfjm
#include "Rexception.hpp"
30
 
31 10 wfjm
using namespace std;
32
 
33
/*!
34
  \class Retro::RosPrintBvi
35
  \brief FIXME_docs.
36
*/
37
 
38 19 wfjm
// all method definitions in namespace Retro
39
namespace Retro {
40
 
41 10 wfjm
//------------------------------------------+-----------------------------------
42
//! Constructor. FIXME_docs
43
 
44
RosPrintBvi::RosPrintBvi(uint8_t val, size_t base, size_t nbit)
45
  : fVal((uint8_t)val),
46
    fBase(base),
47
    fNbit(nbit)
48
{
49
  if (base!=0 && base!=2 && base!=8 && base!=16)
50 19 wfjm
    throw Rexception("RosPrintBvi::<ctor>",
51
                     "Bad args: base must be 0,2,8, or 16");
52 10 wfjm
  if (nbit<1 || nbit>8)
53 19 wfjm
    throw Rexception("RosPrintBvi::<ctor>",
54
                     "Bad args: nbit must be in 1,..,8");
55 10 wfjm
}
56
 
57
//------------------------------------------+-----------------------------------
58
//! Constructor. FIXME_docs
59
 
60
RosPrintBvi::RosPrintBvi(uint16_t val, size_t base, size_t nbit)
61
  : fVal((uint16_t)val),
62
    fBase(base),
63
    fNbit(nbit)
64
{
65
  if (base!=0 && base!=2 && base!=8 && base!=16)
66 19 wfjm
    throw Rexception("RosPrintBvi::<ctor>",
67
                     "Bad args: base must be 0,2,8, or 16");
68 10 wfjm
  if (nbit<1 || nbit>16)
69 19 wfjm
    throw Rexception("RosPrintBvi::<ctor>",
70
                     "Bad args: nbit must be in 1,..,16");
71 10 wfjm
}
72
 
73
//------------------------------------------+-----------------------------------
74
//! Constructor. FIXME_docs
75
 
76
RosPrintBvi::RosPrintBvi(uint32_t val, size_t base, size_t nbit)
77
  : fVal(val),
78
    fBase(base),
79
    fNbit(nbit)
80
{
81
  if (base!=0 && base!=2 && base!=8 && base!=16)
82 19 wfjm
    throw Rexception("RosPrintBvi::<ctor>",
83
                     "Bad args: base must be 0,2,8, or 16");
84 10 wfjm
  if (nbit<1 || nbit>32)
85 19 wfjm
    throw Rexception("RosPrintBvi::<ctor>",
86
                     "Bad args: nbit must be in 1,..,32");
87 10 wfjm
}
88
 
89
//------------------------------------------+-----------------------------------
90
//! FIXME_docs
91
 
92
void RosPrintBvi::Print(std::ostream& os) const
93
{
94
  if (fBase == 0) {
95
    os << RosPrintBvi(fVal, 16, fNbit) << "  "
96
       << RosPrintBvi(fVal,  8, fNbit) << "  "
97
       << RosPrintBvi(fVal,  2, fNbit);
98
    return;
99
  }
100
 
101
  char buf[33];
102
  Convert(buf);
103
  os << buf;
104
  return;
105
}
106
 
107
//------------------------------------------+-----------------------------------
108
//! FIXME_docs
109
 
110
void RosPrintBvi::Print(std::string& os) const
111
{
112
  if (fBase == 0) {
113
    os << RosPrintBvi(fVal, 16, fNbit);
114
    os += "  ";
115
    os << RosPrintBvi(fVal,  8, fNbit);
116
    os += "  ";
117
    os << RosPrintBvi(fVal,  2, fNbit);
118
    return;
119
  }
120
 
121
  char buf[33];
122
  Convert(buf);
123
  os += buf;
124
  return;
125
}
126
 
127
//------------------------------------------+-----------------------------------
128
//! FIXME_docs
129
 
130
void RosPrintBvi::Convert(char* pbuf) const
131
{
132
 
133
  size_t nwidth = 1;
134
  if (fBase ==  8) nwidth = 3;
135
  if (fBase == 16) nwidth = 4;
136
  uint32_t nmask = (1<<nwidth)-1;
137
 
138
  size_t ndig = (fNbit+nwidth-1)/nwidth;
139
 
140
  for (size_t i=ndig; i>0; i--) {
141
    uint32_t nibble = ((fVal)>>((i-1)*nwidth)) & nmask;
142
    nibble += (nibble <= 9) ? '0' : ('a'-10);
143
    *pbuf++ = (char) nibble;
144
  }
145
 
146
  *pbuf++ = '\0';
147
 
148
  return;
149
}
150
 
151 19 wfjm
} // end namespace Retro
152
 

powered by: WebSVN 2.1.0

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