473,804 Members | 3,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implementation approach ..

The member variable addr2 -below- is the address received from a
_vendor_ function (not shown). For discussion purposes lets assume the
address is 0xCFF44000

The code:

typedef unsigned int UINT32;
#define LLSB(x) ((x) & 0xff)
#define LNLSB(x) (((x) >> 8) & 0xff)
#define LNMSB(x) (((x) >> 16) & 0xff)
#define LMSB(x) (((x) >> 24) & 0xff)

UINT32 LongSwap(UINT32 x) {
return (LLSB(x) << 24) |
(LNLSB(x) << 16) |
(LNMSB(x) << 8) | LMSB(x);
}

struct Register {
operator UINT32() {
return *reinterpret_ca st<uint32_t *>(this);
}
void set(UINT32 n) {
*reinterpret_ca st<uint32_t *>(this)=LongSw ap(n);
}
};

// register2 is comprised of two 16 bit values..
struct register2 : public Register{
UINT32 start_vec : 16;
UINT32 stop_vec : 16;
};

// later

UINT32 Bar2Addr;
UINT32 *cReg2Addr;
register2* reg2;

STATUS initialize()
{
STATUS stat;
UINT32 addr2 = 0xCFF44000;

cReg2Addr = (UINT32 *)addr2;
cReg2Addr += 4;
reg2 = (register2*)cRe g2Addr ; // map reg2 pointer to addr2
}

void write_to_reg2(
unsigned short val1,
unsigned short val2
)
{
register2 reg;
reg.start_vec = val1;
reg.stop_vec = val2;
reg2->set(UINT32(reg )); // write val1 and val2 to address but first
endian conversion..
}

Prior to modifying the contents of the address (cReg2Addr) provided, I
must perform an endian conversion (hence the longSwap). The question.
Am I on the right track as far as my approach?

Thanks in advance.

Aug 28 '05 #1
4 1647
ma******@pegasu s.cc.ucf.edu wrote:
The member variable addr2 -below- is the address received from a
_vendor_ function (not shown). For discussion purposes lets assume
the address is 0xCFF44000

The code:

typedef unsigned int UINT32;
I take it you also need (or assume)

typedef UINT32 uint32_t;
#define LLSB(x) ((x) & 0xff)
#define LNLSB(x) (((x) >> 8) & 0xff)
#define LNMSB(x) (((x) >> 16) & 0xff)
#define LMSB(x) (((x) >> 24) & 0xff)

UINT32 LongSwap(UINT32 x) {
return (LLSB(x) << 24) |
(LNLSB(x) << 16) |
(LNMSB(x) << 8) | LMSB(x);
}

struct Register {
operator UINT32() {
return *reinterpret_ca st<uint32_t *>(this);
This is nonsense. You're extracting a 'uint32_t' from something
that is _not_ a 'uint32_t'. Your program has undefined behaviour.
}
void set(UINT32 n) {
*reinterpret_ca st<uint32_t *>(this)=LongSw ap(n);
}
Your struct Register does not have any memory where you could store
the 'uint32_t' value you get from LongSwap. If you retinterpret_ca st
'this' to 'uint32_t*', the storage won't appear from thin air. You
need at least a non-static member variable large enough to contain
a 'uint32_t' value. Hence I have a question: why not have a member
_of_type_ 'uint32_t' and not bother casting anything?

void set(UINT32 n) {
mem_var = LongSwap(n);
}

uint32_t mem_var;

And, of course, the operator UIN32() should look like this:

operator UINT32() const { return mem_var; }

No 'reinterpret_ca st' bullshit.
};

// register2 is comprised of two 16 bit values..
struct register2 : public Register{
UINT32 start_vec : 16;
UINT32 stop_vec : 16;
};

// later

UINT32 Bar2Addr;
UINT32 *cReg2Addr;
register2* reg2;

STATUS initialize()
{
STATUS stat;
UINT32 addr2 = 0xCFF44000;

cReg2Addr = (UINT32 *)addr2;
Using C-style cast where you _really_ need 'reinterpret_ca st' is
rather strange. Use the proper constructs where they are due.
cReg2Addr += 4;
reg2 = (register2*)cRe g2Addr ; // map reg2 pointer to addr2
Again, a C-style cast. Why?
}

void write_to_reg2(
unsigned short val1,
unsigned short val2
)
{
register2 reg;
reg.start_vec = val1;
reg.stop_vec = val2;
reg2->set(UINT32(reg )); // write val1 and val2 to address but first
endian conversion..
}

Prior to modifying the contents of the address (cReg2Addr) provided, I
must perform an endian conversion (hence the longSwap). The question.
Am I on the right track as far as my approach?


What's the ultimate goal? I have to be honest with you, I am losing
track because of all those C-style casts and reinterpret_cas ts applied
so freely and without merit. And why do you need to perform an endian
conversion on an address? That's something I've never heard of.

V
Aug 29 '05 #2
Hola, Victor,

|| typedef UINT32 uint32_t;
My apologies... All uni32_t should reflect UINT32.

void set(UINT32 n) {
mem_var = LongSwap(n);
}

|| uint32_t mem_var;
|| And, of course, the operator UIN32() should look like this:
|| operator UINT32() const { return mem_var; }
|| No 'reinterpret_ca st' bullshit.

Quite vocal there with the use of 'bull..' but I get the point.

|| What's the ultimate goal?

Well here's a synopsis of the current approach. ...... Recall that the
member variable addr2 is obtained from a vendor function. So now

typedef unsigned int UINT32;
UINT32 Bar2Addr;
UINT32 *cReg2Addr;

bool initialize()
{
UINT32 addr2 = 0xCFF44000; // well assume addr2 is 0xCFF44000
cReg2Addr = (UINT32 *)addr2;
cReg2Addr += 4; // at this point cReg2Addr is
'alive and well'.
}

// later
void write_to_reg2(U INT32 start_vec, UINT32 stop_vec)
{
UINT32 reg2;
reg2 = start_vec & 0xffff;
reg2 |= ((stop_vec + start_vec) & 0xffff) <<16;

*cReg2Addr = LongSwap (reg2); // [1] Write a 32 bit value to the
'address'.
// First we must
LongSwap the value because of endian differences..
}

The line inidicated by [1] will set the address with the desired 'start
and stop' vector values. Before I do that I must convert the value
prior to writing the value to the address. In effect, I have a local
variable (reg2) which gets converted then the converted value gets
written to/stored at cReg2Addr. This approach works but becomes a real
pain in the rear. The shifting and anding is enough to make my head
spin.

The 'documentation' has a defintion for register2. So why not do:
struct register2 {
unsigned int start_vec;
unsigned int stop_vec;
};

Within the initialize function. I could now do:
register2 *ptr_reg2;

bool initialize()
{
UINT32 addr2 = 0xCFF44000;
cReg2Addr = (UINT32 *)addr2;
cReg2Addr += 4;

ptr_reg2 = (register2*)cRe g2Addr; // watch this line... Just map
the struct (register2) definition to the address.
// I suspect
placement new would work here but that's seems like overkill
}

The write_to_reg2 function then becomes:

void write_to_reg2(U INT32 start_vec, UINT32 stop_vec)
{
register2 reg;
reg.start_vec = start_vec;
reg.stop_vec = stop_vec;

//ptr_reg2 = reg;
}

The commented out line above brings up an interesting dilema. I must
first LongSwap (reg) then assign it to ptr_reg2, hence my previous
solution which I suspect is convoluted. Your thougths.
Thanks Vic.

Aug 29 '05 #3
ma******@pegasu s.cc.ucf.edu wrote:
Hola, Victor,
What's the ultimate goal?

Well here's a synopsis of the current approach. ...... Recall that
the member variable addr2 is obtained from a vendor function. So
now

typedef unsigned int UINT32;
UINT32 Bar2Addr;
UINT32 *cReg2Addr;

bool initialize()
{
UINT32 addr2 = 0xCFF44000; // well assume addr2 is 0xCFF44000
cReg2Addr = (UINT32 *)addr2;
cReg2Addr += 4; // at this point cReg2Addr is
'alive and well'.
}

// later
void write_to_reg2(U INT32 start_vec, UINT32 stop_vec)
{
UINT32 reg2;
reg2 = start_vec & 0xffff;
reg2 |= ((stop_vec + start_vec) & 0xffff) <<16;

*cReg2Addr = LongSwap (reg2); // [1] Write a 32 bit value to the
'address'.
// First we must
LongSwap the value because of endian differences..
}


This particular thing makes me shudder. If you get an address, it's
gotta be for the same system. If it's for the same system, how can
it have different endian-ness? And if it's not from the same system,
why is _an address_ passed around?

If it's not an address, then why are you treating it as one?
The line inidicated by [1] will set the address with the desired
'start and stop' vector values.
So, it _is_ an address? Is this whole thing an emulator of some kind?
Before I do that I must convert the
value prior to writing the value to the address. In effect, I have a
local variable (reg2) which gets converted then the converted value
gets written to/stored at cReg2Addr. This approach works but becomes
a real pain in the rear. The shifting and anding is enough to make
my head spin.

The 'documentation' has a defintion for register2. So why not do:
struct register2 {
unsigned int start_vec;
unsigned int stop_vec;
};

Within the initialize function. I could now do:
register2 *ptr_reg2;
Why a _pointer_? This is another part where you lose me.
bool initialize()
{
UINT32 addr2 = 0xCFF44000;
cReg2Addr = (UINT32 *)addr2;
cReg2Addr += 4;
You're adding _16_ bytes offset here. Is that intentional?
ptr_reg2 = (register2*)cRe g2Addr; // watch this line... Just map
the struct (register2) definition to the address.
// I suspect
placement new would work here but that's seems like overkill
}

The write_to_reg2 function then becomes:

void write_to_reg2(U INT32 start_vec, UINT32 stop_vec)
{
register2 reg;
reg.start_vec = start_vec;
reg.stop_vec = stop_vec;

//ptr_reg2 = reg;
}

The commented out line above brings up an interesting dilema. I must
first LongSwap (reg) then assign it to ptr_reg2, hence my previous
solution which I suspect is convoluted. Your thougths.


I've really got none. First, it's too late on Sunday night. Second,
if you got it to work, leave it. If it doesn't work, then start to
worry. 'Nough thinking. Time to go to bed.

V
Aug 29 '05 #4

|| Is this whole thing an emulator of some kind?
Part of this, I suspect is my terminology is all over the place. What
it amounts to is I have an address that's mapped across PCI space. The
address that I'm writing to is _really_ an FPGA address (the
documentation calls it register). The vendor function (not shown in
my earlier posts) returns to me the address. So in my local memory I
'fiddle with things'. I LongSwap (becasue of endian differences) the
value then update the contents of the FPGA address/register.
Thanks Vic. You gave me some clues.. I could certainly simply the
register struct to rid/eliminate the reinterpret_cas ts. As always I
tend to over do it with all the reinterpret_cas t mayhem i was doing.

Aug 29 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4414
by: Flavio Ribeiro | last post by:
Hi, Most of my application's operations (including SQL queries and numerical computation) can be cached. An LRU (least recently used) cache would fit the bill perfectly. This LRU cache should be preserved over multiple requests from the same session. If possible, it should be seen across multiple sessions so that all users can benefit from it.
6
2926
by: MPowell | last post by:
Lets suppose the return value on new_command is MSG2_STATUS My intent then is to memcpy the data via the call to CMDHolder and have a function that'll retrieve a copy of the 'stored' data. The latter I'm unsure how to do. Any help appreaciated. #include <iostream> #include <map>
13
1687
by: ma740988 | last post by:
I'm faced with endian issues during communication across different platforms. So I've recommendations/tips (from one of these newsgroups - cant recall which) on an approach (or two or three) that seems reasonable and is based on issues that were encountered and how to handle them in this situation. One of which - the one I'm currently investigating - is as follows:
3
1772
by: masood.iqbal | last post by:
In this day and age, you never say no to any work that is thrown at you ---- so when I was offered this short-term contract to convert legacy C code to C++, I did not say no. Personally I believed that it was a somewhat futile exercise since one of the main requirements was for the existing API (a functional interface written in C) to remain the same. I would have much rathered that the mandate be ab-initio, but that was not the case...
4
1561
by: Malkocoglu | last post by:
In the good old days , i had a class that had 30 functions (let's say) There was a single include(*.H) file and i could have several implementation(*.CPP) files The reason for doing this is to have some functions grouped so it is easier to read/manage them... Maybe several developers work on the same class but on different parts of it in parallel... I have a C_foo class foo.h // the header file that has the C_foo class...
13
1605
by: Ankit | last post by:
Hello, I have an old VC++ project code base which I am trying to build and use. This uses an ostream object. Now in my project, I have overloaded the leftshift operator ( << ), basically being used to "put" data to the stream object. However, while I run the app, it does not call the correct implementation of the operator. For example, say I have following piece of code: ostream o;
110
8631
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm almost halfway through writing the over 200 functions needed to implement C99's version of math.h, and I would like to have some feedback and/or expert advice on my implementations. Sincerely, Gregory Pietsch
20
6091
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this, as we seem to lack only a single 'step' to have "full separation"... We have a first project, namespace Ninterface, that contains the interface definitions in class1_interface.cs, like this: namespace Ninterface { public interface IClass1{
6
3969
by: Ralph | last post by:
Hi, I was reading effictive C++ and some other books again and they all tell you about hiding implementation details (proxy/pimpl/inheritance) but they never really explain when to use it. I am starting on a new project which is part library so I think it would be good to hide the implementation for the public classes in the library but this seems a lot of overhead to me (both when developing and runtime overhead).
2
1909
by: puzzlecracker | last post by:
I don't see the purpose of explicit interface implementation other than to hide its signature in the class that implements it, and, instead, write your own, perhaps with a different signature, implementation. Also, if you implement more than two interfaces with the same method, which is rarity in practice or so it seems. To me it appears a shallow end... Am I missing something? Thanks
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.