473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

bitwise rotation

Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
....
public uint Value;
....
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I
presume I'd have to have knowledge of the intended size of the value and do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn
Nov 15 '05 #1
33 2498
How about:

public uint RotateRight16(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (16-Count)))&0xffff;
}
public uint RotateRight8(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (8-Count)))&0xff;
}

public uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (16-Count)))&0xffff;
}
public uint RotateLeft8(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (8-Count)))&0xff;
}

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:O9***************@TK2MSFTNGP11.phx.gbl...
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I
presume I'd have to have knowledge of the intended size of the value and do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn

Nov 15 '05 #2
Oops, forgot to make sure that value is within limits:

public static uint RotateRight16(uint Value, byte Count)
{
return (((Value&0xffff) >> Count) | (Value << (16-Count)))&0xffff;
}
public static uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | ((Value&0xffff) >> (16-Count)))&0xffff;
}

etc for 8-bit.

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message news:eE**************@TK2MSFTNGP11.phx.gbl...
How about:

public uint RotateRight16(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (16-Count)))&0xffff;
}
public uint RotateRight8(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (8-Count)))&0xff;
}

public uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (16-Count)))&0xffff;
}
public uint RotateLeft8(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (8-Count)))&0xff;
}

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:O9***************@TK2MSFTNGP11.phx.gbl...
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I
presume I'd have to have knowledge of the intended size of the value and do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn


Nov 15 '05 #3
Hard to believe it was so simple, I tried that... except where you're
16-count is, I left it at 64 bits with the 8-bit mask, that explains why it
was wrong.

Thanks very much,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:eE**************@TK2MSFTNGP11.phx.gbl...
How about:

public uint RotateRight16(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (16-Count)))&0xffff;
}
public uint RotateRight8(uint Value, byte Count)
{
return ((Value >> Count) | (Value << (8-Count)))&0xff;
}

public uint RotateLeft16(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (16-Count)))&0xffff;
}
public uint RotateLeft8(uint Value, byte Count)
{
return ((Value << Count) | (Value >> (8-Count)))&0xff;
}

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:O9***************@TK2MSFTNGP11.phx.gbl...
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I presume I'd have to have knowledge of the intended size of the value and do the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn


Nov 15 '05 #4
So, you're writing an emulator for which processor??

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Shawn B." <le****@html.com> wrote in message
news:O9*************@TK2MSFTNGP11.phx.gbl...
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as well. I presume I'd have to have knowledge of the intended size of the value and do the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn

Nov 15 '05 #5
Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
16-bit 65816. In particular, I'm doing this so I can debug (I have the
ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware
(such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
plugins can treat an area of memory as a dedicated "something" such as text
display or graphics display, etc. For now, I'm concerned not even with the
text display. My end result, is actually more akin to being able to debug
my NES games I've been into writing lately. Also I want to be ready with my
own debugger for Andre LaMothe's upcoming XGameStation console (don't know
whether he'll have a debugger or not) but I want a debugger that allows me
to change the code while it's being debugged and my simulator allows for it
(as long as the source code file is available -- else if it was loading as a
..bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable)
assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast.
I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm
primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
but also wanted it to target the .NET framework (as much as is reasonable
and I'll find out what that really means when its mature enough to start
using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
16-bit processor as I haven't deciphered the specs for the CPU yet (the
8-bit CPU is very easy compared to the 16-bit version of the processor). I
ultimately will make this Interfacable so I can write a Z80 core, as well.

These bit manipulations are the utmost inner-loop so I have to make them
tight. Looking at the JIT'd code for these methods seems to be decent but I
have to make too many function calls (read, jumps) for my tastes but doesn't
seem to be hurting me at the moment). Since I can't inline in .NET, I'll
have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate
8-bit and 16-bit shifts and rotates. I'm also trying to work out a better
way for simulating memory manipulation. Now I'm creating a string of (64kb
in length) but manipulating each byte by substrings has got to be expensive.
I'm looking into whether a byte array would be better.
Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string
of 1's and 0's). I know now have to read the binary back to a numeric, and
allow a way to get/set/toggle individual bits.
Thanks,
Shawn
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
So, you're writing an emulator for which processor??

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Shawn B." <le****@html.com> wrote in message
news:O9*************@TK2MSFTNGP11.phx.gbl...
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as
well. I
presume I'd have to have knowledge of the intended size of the value and

do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and

clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn


Nov 15 '05 #6
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:OW**************@tk2msftngp13.phx.gbl...
Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
16-bit 65816. In particular, I'm doing this so I can debug (I have the
ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware
(such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
plugins can treat an area of memory as a dedicated "something" such as text
display or graphics display, etc. For now, I'm concerned not even with the
text display. My end result, is actually more akin to being able to debug
my NES games I've been into writing lately. Also I want to be ready with my
own debugger for Andre LaMothe's upcoming XGameStation console (don't know
whether he'll have a debugger or not) but I want a debugger that allows me
to change the code while it's being debugged and my simulator allows for it
(as long as the source code file is available -- else if it was loading as a
.bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable)
assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast.
I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm
primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
but also wanted it to target the .NET framework (as much as is reasonable
and I'll find out what that really means when its mature enough to start
using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
16-bit processor as I haven't deciphered the specs for the CPU yet (the
8-bit CPU is very easy compared to the 16-bit version of the processor). I
ultimately will make this Interfacable so I can write a Z80 core, as well.

These bit manipulations are the utmost inner-loop so I have to make them
tight. Looking at the JIT'd code for these methods seems to be decent but I
have to make too many function calls (read, jumps) for my tastes but doesn't
seem to be hurting me at the moment). Since I can't inline in .NET, I'll
have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate
8-bit and 16-bit shifts and rotates. I'm also trying to work out a better
way for simulating memory manipulation. Now I'm creating a string of (64kb
in length) but manipulating each byte by substrings has got to be expensive.
I'm looking into whether a byte array would be better.
Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string
of 1's and 0's). I know now have to read the binary back to a numeric, and
allow a way to get/set/toggle individual bits.
Thanks,
Shawn
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
So, you're writing an emulator for which processor??

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Shawn B." <le****@html.com> wrote in message
news:O9*************@TK2MSFTNGP11.phx.gbl...
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the
shift operator). I need it to work on an 8-bit or 16-bit value, as

well.
I
presume I'd have to have knowledge of the intended size of the value and

do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and

clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
value?
Thanks,
Shawn



Nov 15 '05 #7
Robert,

Youre suggestions have transformed my operations into 1-liners from 5
liners... you'll be the first to be notified (on these groups, of course).

I've opted to create a series of classes, Binary8, Binary16, Binary32,
Binary64 (all are unsigned).

I need to know, when you say "int x = 10;" is that a compiler trick or is
there a way to create my own type that can do that, for example, "Binary8 x
= 0xFF;"???
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the 16-bit 65816. In particular, I'm doing this so I can debug (I have the
ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware
(such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my plugins can treat an area of memory as a dedicated "something" such as text display or graphics display, etc. For now, I'm concerned not even with the text display. My end result, is actually more akin to being able to debug my NES games I've been into writing lately. Also I want to be ready with my own debugger for Andre LaMothe's upcoming XGameStation console (don't know whether he'll have a debugger or not) but I want a debugger that allows me to change the code while it's being debugged and my simulator allows for it (as long as the source code file is available -- else if it was loading as a .bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable) assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast. I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm primarly a VB.NET programmer) but didn't feel like doing it in Managed C++ but also wanted it to target the .NET framework (as much as is reasonable and I'll find out what that really means when its mature enough to start
using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the 16-bit processor as I haven't deciphered the specs for the CPU yet (the
8-bit CPU is very easy compared to the 16-bit version of the processor). I ultimately will make this Interfacable so I can write a Z80 core, as well.
These bit manipulations are the utmost inner-loop so I have to make them
tight. Looking at the JIT'd code for these methods seems to be decent but I have to make too many function calls (read, jumps) for my tastes but doesn't seem to be hurting me at the moment). Since I can't inline in .NET, I'll have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate 8-bit and 16-bit shifts and rotates. I'm also trying to work out a better way for simulating memory manipulation. Now I'm creating a string of (64kb in length) but manipulating each byte by substrings has got to be expensive. I'm looking into whether a byte array would be better.
Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string of 1's and 0's). I know now have to read the binary back to a numeric, and allow a way to get/set/toggle individual bits.
Thanks,
Shawn
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
So, you're writing an emulator for which processor??

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Shawn B." <le****@html.com> wrote in message
news:O9*************@TK2MSFTNGP11.phx.gbl...
> Greetings,
>
> I am simulating an assembly language bit rotation in C# and it works
> wonderfully
>
> ---------
> ...
> public uint Value;
> ...
> public uint RotateRight(byte count) {
> Value = ((Value >> count) | (Value << (32-count)));
> return Value;
> }
> ---------
>
> as long is the value is 32-bit or 64-bit (because of a limitation of the > shift operator). I need it to work on an 8-bit or 16-bit value, as

well.
I
> presume I'd have to have knowledge of the intended size of the value and do
> the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
> the extra bits if there are any. That seems like a lot of work.
>
> Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit > value?
>
>
> Thanks,
> Shawn
>
>



Nov 15 '05 #8
Shawn B. <le****@html.com> wrote:
I need to know, when you say "int x = 10;" is that a compiler trick or is
there a way to create my own type that can do that, for example, "Binary8 x
= 0xFF;"???


It's not really a compiler trick, it's just that the type of 10 is int,
so the compiler can just assign that value to the variable.

I believe if you give the Binary8 class an implicit conversion from
int, you could get away with it.

I'm not sure whether or not you could get away with giving an implicit
conversion from byte, letting the compiler figure out that it can
convert a constant expression of type int to byte, and then from byte
to Binary8 - if that works though, it would remove the need for range
checking in your conversion.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
In addition to Jon's response,
No, you can't overload the = operator.
Found a nice summary of what can be overloaded:
http://www.csharphelp.com/archives/archive135.html

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:ez**************@tk2msftngp13.phx.gbl...
Robert,

Youre suggestions have transformed my operations into 1-liners from 5
liners... you'll be the first to be notified (on these groups, of course).

I've opted to create a series of classes, Binary8, Binary16, Binary32,
Binary64 (all are unsigned).

I need to know, when you say "int x = 10;" is that a compiler trick or is
there a way to create my own type that can do that, for example, "Binary8 x
= 0xFF;"???
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the 16-bit 65816. In particular, I'm doing this so I can debug (I have the
ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware
(such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my plugins can treat an area of memory as a dedicated "something" such as text display or graphics display, etc. For now, I'm concerned not even with the text display. My end result, is actually more akin to being able to debug my NES games I've been into writing lately. Also I want to be ready with my own debugger for Andre LaMothe's upcoming XGameStation console (don't know whether he'll have a debugger or not) but I want a debugger that allows me to change the code while it's being debugged and my simulator allows for it (as long as the source code file is available -- else if it was loading as a .bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable) assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast. I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm primarly a VB.NET programmer) but didn't feel like doing it in Managed C++ but also wanted it to target the .NET framework (as much as is reasonable and I'll find out what that really means when its mature enough to start
using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the 16-bit processor as I haven't deciphered the specs for the CPU yet (the
8-bit CPU is very easy compared to the 16-bit version of the processor). I ultimately will make this Interfacable so I can write a Z80 core, as well.
These bit manipulations are the utmost inner-loop so I have to make them
tight. Looking at the JIT'd code for these methods seems to be decent but I have to make too many function calls (read, jumps) for my tastes but doesn't seem to be hurting me at the moment). Since I can't inline in .NET, I'll have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate 8-bit and 16-bit shifts and rotates. I'm also trying to work out a better way for simulating memory manipulation. Now I'm creating a string of (64kb in length) but manipulating each byte by substrings has got to be expensive. I'm looking into whether a byte array would be better.
Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string of 1's and 0's). I know now have to read the binary back to a numeric, and allow a way to get/set/toggle individual bits.
Thanks,
Shawn
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> So, you're writing an emulator for which processor??
>
> --
> Bob Powell [MVP]
> C#, System.Drawing
>
> September's edition of Well Formed is now available.
> http://www.bobpowell.net/currentissue.htm
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/gdiplus_faq.htm
>
>
>
> "Shawn B." <le****@html.com> wrote in message
> news:O9*************@TK2MSFTNGP11.phx.gbl...
> > Greetings,
> >
> > I am simulating an assembly language bit rotation in C# and it works
> > wonderfully
> >
> > ---------
> > ...
> > public uint Value;
> > ...
> > public uint RotateRight(byte count) {
> > Value = ((Value >> count) | (Value << (32-count)));
> > return Value;
> > }
> > ---------
> >
> > as long is the value is 32-bit or 64-bit (because of a limitation of the > > shift operator). I need it to work on an 8-bit or 16-bit value, as
well.
> I
> > presume I'd have to have knowledge of the intended size of the value and > do
> > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and > clear
> > the extra bits if there are any. That seems like a lot of work.
> >
> > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit > > value?
> >
> >
> > Thanks,
> > Shawn
> >
> >
>
>



Nov 15 '05 #10
Instead of creating classes, use structs. Or, if you want the best
performance, just use the unsigned integers supported by .NET and C#.
Manipulate the integers using static methods.

Your BinaryX classes are really just unsigned integers with some added
functionality.

"Shawn B." <le****@html.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
Robert,

Youre suggestions have transformed my operations into 1-liners from 5
liners... you'll be the first to be notified (on these groups, of course).

I've opted to create a series of classes, Binary8, Binary16, Binary32,
Binary64 (all are unsigned).

I need to know, when you say "int x = 10;" is that a compiler trick or is
there a way to create my own type that can do that, for example, "Binary8 x = 0xFF;"???
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:OW**************@tk2msftngp13.phx.gbl...
Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the 16-bit 65816. In particular, I'm doing this so I can debug (I have the ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
plugins can treat an area of memory as a dedicated "something" such as text display or graphics display, etc. For now, I'm concerned not even
with
the text display. My end result, is actually more akin to being able to debug my NES games I've been into writing lately. Also I want to be ready with my own debugger for Andre LaMothe's upcoming XGameStation console (don't know whether he'll have a debugger or not) but I want a debugger that
allows
me to change the code while it's being debugged and my simulator allows
for
it (as long as the source code file is available -- else if it was
loading
as a .bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable) assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast. I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm primarly a VB.NET programmer) but didn't feel like doing it in Managed C++ but also wanted it to target the .NET framework (as much as is reasonable and I'll find out what that really means when its mature enough to
start using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating
the 16-bit processor as I haven't deciphered the specs for the CPU yet
(the 8-bit CPU is very easy compared to the 16-bit version of the processor).
I ultimately will make this Interfacable so I can write a Z80 core, as well.
These bit manipulations are the utmost inner-loop so I have to make
them tight. Looking at the JIT'd code for these methods seems to be decent
but I have to make too many function calls (read, jumps) for my tastes but doesn't seem to be hurting me at the moment). Since I can't inline in .NET, I'll have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate 8-bit and 16-bit shifts and rotates. I'm also trying to work out a better way for simulating memory manipulation. Now I'm creating a string of (64kb in length) but manipulating each byte by substrings has got to be expensive. I'm looking into whether a byte array would be better.
Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string of 1's and 0's). I know now have to read the binary back to a numeric, and
allow a way to get/set/toggle individual bits.
Thanks,
Shawn
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> So, you're writing an emulator for which processor??
>
> --
> Bob Powell [MVP]
> C#, System.Drawing
>
> September's edition of Well Formed is now available.
> http://www.bobpowell.net/currentissue.htm
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/gdiplus_faq.htm
>
>
>
> "Shawn B." <le****@html.com> wrote in message
> news:O9*************@TK2MSFTNGP11.phx.gbl...
> > Greetings,
> >
> > I am simulating an assembly language bit rotation in C# and it
works > > wonderfully
> >
> > ---------
> > ...
> > public uint Value;
> > ...
> > public uint RotateRight(byte count) {
> > Value = ((Value >> count) | (Value << (32-count)));
> > return Value;
> > }
> > ---------
> >
> > as long is the value is 32-bit or 64-bit (because of a limitation of
the > > shift operator). I need it to work on an 8-bit or 16-bit value,
as well.
> I
> > presume I'd have to have knowledge of the intended size of the
value and > do
> > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and > clear
> > the extra bits if there are any. That seems like a lot of work.
> >
> > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit > > value?
> >
> >
> > Thanks,
> > Shawn
> >
> >
>
>



Nov 15 '05 #11
Cool. I started on one for the Z80 a while back and I wanted to write a
Sinclair / Timex Spectrum emulator.

Some years ago I wrote a complete disassembler for the z80 for the Grundy
corporation (newbrain) before they went belly up so I got to know the
instruction set inside-out.

Great project. I'd like to keep informed of it if you don't mind.

Regards,

Bob.
--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Shawn B." <le****@html.com> wrote in message
news:OW**************@tk2msftngp13.phx.gbl...
Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
16-bit 65816. In particular, I'm doing this so I can debug (I have the
ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware
(such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my plugins can treat an area of memory as a dedicated "something" such as text display or graphics display, etc. For now, I'm concerned not even with the text display. My end result, is actually more akin to being able to debug
my NES games I've been into writing lately. Also I want to be ready with my own debugger for Andre LaMothe's upcoming XGameStation console (don't know
whether he'll have a debugger or not) but I want a debugger that allows me
to change the code while it's being debugged and my simulator allows for it (as long as the source code file is available -- else if it was loading as a .bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable)
assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast .. I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
but also wanted it to target the .NET framework (as much as is reasonable
and I'll find out what that really means when its mature enough to start
using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the 16-bit processor as I haven't deciphered the specs for the CPU yet (the
8-bit CPU is very easy compared to the 16-bit version of the processor). I ultimately will make this Interfacable so I can write a Z80 core, as well.

These bit manipulations are the utmost inner-loop so I have to make them
tight. Looking at the JIT'd code for these methods seems to be decent but I have to make too many function calls (read, jumps) for my tastes but doesn't seem to be hurting me at the moment). Since I can't inline in .NET, I'll
have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate 8-bit and 16-bit shifts and rotates. I'm also trying to work out a better
way for simulating memory manipulation. Now I'm creating a string of (64kb in length) but manipulating each byte by substrings has got to be expensive. I'm looking into whether a byte array would be better.
Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string
of 1's and 0's). I know now have to read the binary back to a numeric, and allow a way to get/set/toggle individual bits.
Thanks,
Shawn
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
So, you're writing an emulator for which processor??

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Shawn B." <le****@html.com> wrote in message
news:O9*************@TK2MSFTNGP11.phx.gbl...
Greetings,

I am simulating an assembly language bit rotation in C# and it works
wonderfully

---------
...
public uint Value;
...
public uint RotateRight(byte count) {
Value = ((Value >> count) | (Value << (32-count)));
return Value;
}
---------

as long is the value is 32-bit or 64-bit (because of a limitation of the shift operator). I need it to work on an 8-bit or 16-bit value, as well.
I
presume I'd have to have knowledge of the intended size of the value and do
the shift and move the bit and the 0 or 7, or 0 or 15 bit position and

clear
the extra bits if there are any. That seems like a lot of work.

Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or

64-bit value?
Thanks,
Shawn



Nov 15 '05 #12
Yeah, I learned that (you can overload it in MC++)... instead I used the
"implicit" gig.

I am embarking now to overload all the operators for this struct and
implementing IConvertable as well as some other Interfaces and things. I
want to make it as complete as I possibly can since I intend to use it very
intensely.
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
In addition to Jon's response,
No, you can't overload the = operator.
Found a nice summary of what can be overloaded:
http://www.csharphelp.com/archives/archive135.html

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:ez**************@tk2msftngp13.phx.gbl...
Robert,

Youre suggestions have transformed my operations into 1-liners from 5
liners... you'll be the first to be notified (on these groups, of course).
I've opted to create a series of classes, Binary8, Binary16, Binary32,
Binary64 (all are unsigned).

I need to know, when you say "int x = 10;" is that a compiler trick or is there a way to create my own type that can do that, for example, "Binary8 x = 0xFF;"???
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
> Don't miss a thing, d'ya? =))
>
> I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and
the
> 16-bit 65816. In particular, I'm doing this so I can debug (I have
the > ability to modify the assembly language while the debugger is running > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> instructions. I'm not designing it to be a particular piece of hardware > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> plugins can treat an area of memory as a dedicated "something" such
as text
> display or graphics display, etc. For now, I'm concerned not even
with the
> text display. My end result, is actually more akin to being able to

debug
> my NES games I've been into writing lately. Also I want to be ready

with my
> own debugger for Andre LaMothe's upcoming XGameStation console
(don't know
> whether he'll have a debugger or not) but I want a debugger that
allows me
> to change the code while it's being debugged and my simulator allows
for it
> (as long as the source code file is available -- else if it was
loading as a
> .bin file then it won't be editable during a debug session).
>
> So it's really more than a simulator. It's an integrated

(non-seperable)
> assembly, linker, dissassembler, debugger, simulator, etc. kind of a

beast.
> I chose C# because it's not the kind of thing I'd want to do in
VB.NET (I'm
> primarly a VB.NET programmer) but didn't feel like doing it in
Managed C++
> but also wanted it to target the .NET framework (as much as is

reasonable
> and I'll find out what that really means when its mature enough to
start > using reliably).
>
> I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> 16-bit processor as I haven't deciphered the specs for the CPU yet
(the > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> ultimately will make this Interfacable so I can write a Z80 core, as

well.
>
> These bit manipulations are the utmost inner-loop so I have to make
them > tight. Looking at the JIT'd code for these methods seems to be decent but I
> have to make too many function calls (read, jumps) for my tastes but

doesn't
> seem to be hurting me at the moment). Since I can't inline in .NET,

I'll
> have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> generate the best code for the rotates and shifts because I have to

emulate
> 8-bit and 16-bit shifts and rotates. I'm also trying to work out a

better
> way for simulating memory manipulation. Now I'm creating a string
of (64kb
> in length) but manipulating each byte by substrings has got to be

expensive.
> I'm looking into whether a byte array would be better.
>
>
> Thanks for the help,
> if interested, I'll post my binary class here for all to see.
>
> I've completed the Rotates, Shift, converting numerics into binary

(string
> of 1's and 0's). I know now have to read the binary back to a
numeric, and
> allow a way to get/set/toggle individual bits.
>
>
> Thanks,
> Shawn
>
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > So, you're writing an emulator for which processor??
> >
> > --
> > Bob Powell [MVP]
> > C#, System.Drawing
> >
> > September's edition of Well Formed is now available.
> > http://www.bobpowell.net/currentissue.htm
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdiplus_faq.htm
> >
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > Greetings,
> > >
> > > I am simulating an assembly language bit rotation in C# and it
works > > > wonderfully
> > >
> > > ---------
> > > ...
> > > public uint Value;
> > > ...
> > > public uint RotateRight(byte count) {
> > > Value = ((Value >> count) | (Value << (32-count)));
> > > return Value;
> > > }
> > > ---------
> > >
> > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > shift operator). I need it to work on an 8-bit or 16-bit value,
as > well.
> > I
> > > presume I'd have to have knowledge of the intended size of the

value and
> > do
> > > the shift and move the bit and the 0 or 7, or 0 or 15 bit
position and
> > clear
> > > the extra bits if there are any. That seems like a lot of work.
> > >
> > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit
or 64-bit
> > > value?
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> >
> >
>
>



Nov 15 '05 #13
Yes, I had already (in the 3rd major revision of this thing) changed it to
use structs. Each of the Binary8, Binary16, Binary32, and Binary64 are
Byte, ushort, uint, and ulong respectively. If I don't use unsigned then
the rotates crash if rotating a set high or low bit.

Yes, they are just instrinsic valuetypes with added functionality, for that
matter.
Thanks,
Shawn

"Jason Smith" <ja***@nospam.com> wrote in message
news:er**************@TK2MSFTNGP11.phx.gbl...
Instead of creating classes, use structs. Or, if you want the best
performance, just use the unsigned integers supported by .NET and C#.
Manipulate the integers using static methods.

Your BinaryX classes are really just unsigned integers with some added
functionality.

"Shawn B." <le****@html.com> wrote in message
news:ez**************@tk2msftngp13.phx.gbl...
Robert,

Youre suggestions have transformed my operations into 1-liners from 5
liners... you'll be the first to be notified (on these groups, of course).

I've opted to create a series of classes, Binary8, Binary16, Binary32,
Binary64 (all are unsigned).

I need to know, when you say "int x = 10;" is that a compiler trick or is there a way to create my own type that can do that, for example, "Binary8
x
= 0xFF;"???
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:OW**************@tk2msftngp13.phx.gbl...
> Don't miss a thing, d'ya? =))
>
> I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
> 16-bit 65816. In particular, I'm doing this so I can debug (I have the > ability to modify the assembly language while the debugger is
running > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> instructions. I'm not designing it to be a particular piece of

hardware > (such as the Apple, Atari, Commadore, etc.). I can supply it
plugins so
my
> plugins can treat an area of memory as a dedicated "something" such
as text
> display or graphics display, etc. For now, I'm concerned not even with
the
> text display. My end result, is actually more akin to being able to

debug
> my NES games I've been into writing lately. Also I want to be ready

with my
> own debugger for Andre LaMothe's upcoming XGameStation console

(don't know
> whether he'll have a debugger or not) but I want a debugger that allows
me
> to change the code while it's being debugged and my simulator allows

for
it
> (as long as the source code file is available -- else if it was

loading
as a
> .bin file then it won't be editable during a debug session).
>
> So it's really more than a simulator. It's an integrated

(non-seperable)
> assembly, linker, dissassembler, debugger, simulator, etc. kind of a

beast.
> I chose C# because it's not the kind of thing I'd want to do in

VB.NET (I'm
> primarly a VB.NET programmer) but didn't feel like doing it in
Managed C++
> but also wanted it to target the .NET framework (as much as is

reasonable
> and I'll find out what that really means when its mature enough to start > using reliably).
>
> I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating
the
> 16-bit processor as I haven't deciphered the specs for the CPU yet

(the > 8-bit CPU is very easy compared to the 16-bit version of the processor).
I
> ultimately will make this Interfacable so I can write a Z80 core, as

well.
>
> These bit manipulations are the utmost inner-loop so I have to make

them > tight. Looking at the JIT'd code for these methods seems to be
decent but I
> have to make too many function calls (read, jumps) for my tastes but

doesn't
> seem to be hurting me at the moment). Since I can't inline in .NET,

I'll
> have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> generate the best code for the rotates and shifts because I have to

emulate
> 8-bit and 16-bit shifts and rotates. I'm also trying to work out a

better
> way for simulating memory manipulation. Now I'm creating a string
of (64kb
> in length) but manipulating each byte by substrings has got to be

expensive.
> I'm looking into whether a byte array would be better.
>
>
> Thanks for the help,
> if interested, I'll post my binary class here for all to see.
>
> I've completed the Rotates, Shift, converting numerics into binary

(string
> of 1's and 0's). I know now have to read the binary back to a numeric,
and
> allow a way to get/set/toggle individual bits.
>
>
> Thanks,
> Shawn
>
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > So, you're writing an emulator for which processor??
> >
> > --
> > Bob Powell [MVP]
> > C#, System.Drawing
> >
> > September's edition of Well Formed is now available.
> > http://www.bobpowell.net/currentissue.htm
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdiplus_faq.htm
> >
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > Greetings,
> > >
> > > I am simulating an assembly language bit rotation in C# and it

works > > > wonderfully
> > >
> > > ---------
> > > ...
> > > public uint Value;
> > > ...
> > > public uint RotateRight(byte count) {
> > > Value = ((Value >> count) | (Value << (32-count)));
> > > return Value;
> > > }
> > > ---------
> > >
> > > as long is the value is 32-bit or 64-bit (because of a
limitation of
the
> > > shift operator). I need it to work on an 8-bit or 16-bit value,

as > well.
> > I
> > > presume I'd have to have knowledge of the intended size of the

value
and
> > do
> > > the shift and move the bit and the 0 or 7, or 0 or 15 bit

position and
> > clear
> > > the extra bits if there are any. That seems like a lot of work.
> > >
> > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit
or 64-bit
> > > value?
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> >
> >
>
>



Nov 15 '05 #14
BTW:

I am at the point now where I can say:

Binary8 bin = "10101110";
Binary8 bin2 = bin - 0x03;
Binary16 bin3 = bin << 4;
....
Binary32 bin4 = bin3.RotateLeft(7);
Bin4 >>= 1;

bin4.Bit(7) = False; // I'm Thinking about being able to say Bit(x) = ON;
Bit(x) = OFF; Not sure yet, perhaps even
// Bit(x) = 1; Bit(x) = 0;

MessageBox.Show(bin4.ToString());

The ToString() method returns a string of 1's and 0's representing the
binary form of the value.
Binary8.ToString() returns similar "00011111";
Binary8.ToString(False) returns similar "11111"; // no trailing padding of
0's.

I still have to work out the "&" and "|" operators to the the same effect as
"bin.And(0x8A);" or something like that.

Still interested?
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
In addition to Jon's response,
No, you can't overload the = operator.
Found a nice summary of what can be overloaded:
http://www.csharphelp.com/archives/archive135.html

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:ez**************@tk2msftngp13.phx.gbl...
Robert,

Youre suggestions have transformed my operations into 1-liners from 5
liners... you'll be the first to be notified (on these groups, of course).
I've opted to create a series of classes, Binary8, Binary16, Binary32,
Binary64 (all are unsigned).

I need to know, when you say "int x = 10;" is that a compiler trick or is there a way to create my own type that can do that, for example, "Binary8 x = 0xFF;"???
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
> Don't miss a thing, d'ya? =))
>
> I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and
the
> 16-bit 65816. In particular, I'm doing this so I can debug (I have
the > ability to modify the assembly language while the debugger is running > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> instructions. I'm not designing it to be a particular piece of hardware > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> plugins can treat an area of memory as a dedicated "something" such
as text
> display or graphics display, etc. For now, I'm concerned not even
with the
> text display. My end result, is actually more akin to being able to

debug
> my NES games I've been into writing lately. Also I want to be ready

with my
> own debugger for Andre LaMothe's upcoming XGameStation console
(don't know
> whether he'll have a debugger or not) but I want a debugger that
allows me
> to change the code while it's being debugged and my simulator allows
for it
> (as long as the source code file is available -- else if it was
loading as a
> .bin file then it won't be editable during a debug session).
>
> So it's really more than a simulator. It's an integrated

(non-seperable)
> assembly, linker, dissassembler, debugger, simulator, etc. kind of a

beast.
> I chose C# because it's not the kind of thing I'd want to do in
VB.NET (I'm
> primarly a VB.NET programmer) but didn't feel like doing it in
Managed C++
> but also wanted it to target the .NET framework (as much as is

reasonable
> and I'll find out what that really means when its mature enough to
start > using reliably).
>
> I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> 16-bit processor as I haven't deciphered the specs for the CPU yet
(the > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> ultimately will make this Interfacable so I can write a Z80 core, as

well.
>
> These bit manipulations are the utmost inner-loop so I have to make
them > tight. Looking at the JIT'd code for these methods seems to be decent but I
> have to make too many function calls (read, jumps) for my tastes but

doesn't
> seem to be hurting me at the moment). Since I can't inline in .NET,

I'll
> have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> generate the best code for the rotates and shifts because I have to

emulate
> 8-bit and 16-bit shifts and rotates. I'm also trying to work out a

better
> way for simulating memory manipulation. Now I'm creating a string
of (64kb
> in length) but manipulating each byte by substrings has got to be

expensive.
> I'm looking into whether a byte array would be better.
>
>
> Thanks for the help,
> if interested, I'll post my binary class here for all to see.
>
> I've completed the Rotates, Shift, converting numerics into binary

(string
> of 1's and 0's). I know now have to read the binary back to a
numeric, and
> allow a way to get/set/toggle individual bits.
>
>
> Thanks,
> Shawn
>
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > So, you're writing an emulator for which processor??
> >
> > --
> > Bob Powell [MVP]
> > C#, System.Drawing
> >
> > September's edition of Well Formed is now available.
> > http://www.bobpowell.net/currentissue.htm
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdiplus_faq.htm
> >
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > Greetings,
> > >
> > > I am simulating an assembly language bit rotation in C# and it
works > > > wonderfully
> > >
> > > ---------
> > > ...
> > > public uint Value;
> > > ...
> > > public uint RotateRight(byte count) {
> > > Value = ((Value >> count) | (Value << (32-count)));
> > > return Value;
> > > }
> > > ---------
> > >
> > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > shift operator). I need it to work on an 8-bit or 16-bit value,
as > well.
> > I
> > > presume I'd have to have knowledge of the intended size of the

value and
> > do
> > > the shift and move the bit and the 0 or 7, or 0 or 15 bit
position and
> > clear
> > > the extra bits if there are any. That seems like a lot of work.
> > >
> > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit
or 64-bit
> > > value?
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> >
> >
>
>



Nov 15 '05 #15
Well, I've posted it twice and no one is jumpting to optimize it... perhaps
it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the 16-bit 65816. In particular, I'm doing this so I can debug (I have the
ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware
(such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my plugins can treat an area of memory as a dedicated "something" such as text display or graphics display, etc. For now, I'm concerned not even with the text display. My end result, is actually more akin to being able to debug my NES games I've been into writing lately. Also I want to be ready with my own debugger for Andre LaMothe's upcoming XGameStation console (don't know whether he'll have a debugger or not) but I want a debugger that allows me to change the code while it's being debugged and my simulator allows for it (as long as the source code file is available -- else if it was loading as a .bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable) assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast. I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm primarly a VB.NET programmer) but didn't feel like doing it in Managed C++ but also wanted it to target the .NET framework (as much as is reasonable and I'll find out what that really means when its mature enough to start
using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the 16-bit processor as I haven't deciphered the specs for the CPU yet (the
8-bit CPU is very easy compared to the 16-bit version of the processor). I ultimately will make this Interfacable so I can write a Z80 core, as well.
These bit manipulations are the utmost inner-loop so I have to make them
tight. Looking at the JIT'd code for these methods seems to be decent but I have to make too many function calls (read, jumps) for my tastes but doesn't seem to be hurting me at the moment). Since I can't inline in .NET, I'll have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate 8-bit and 16-bit shifts and rotates. I'm also trying to work out a better way for simulating memory manipulation. Now I'm creating a string of (64kb in length) but manipulating each byte by substrings has got to be expensive. I'm looking into whether a byte array would be better.
Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string of 1's and 0's). I know now have to read the binary back to a numeric, and allow a way to get/set/toggle individual bits.
Thanks,
Shawn
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
So, you're writing an emulator for which processor??

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Shawn B." <le****@html.com> wrote in message
news:O9*************@TK2MSFTNGP11.phx.gbl...
> Greetings,
>
> I am simulating an assembly language bit rotation in C# and it works
> wonderfully
>
> ---------
> ...
> public uint Value;
> ...
> public uint RotateRight(byte count) {
> Value = ((Value >> count) | (Value << (32-count)));
> return Value;
> }
> ---------
>
> as long is the value is 32-bit or 64-bit (because of a limitation of the > shift operator). I need it to work on an 8-bit or 16-bit value, as

well.
I
> presume I'd have to have knowledge of the intended size of the value and do
> the shift and move the bit and the 0 or 7, or 0 or 15 bit position and clear
> the extra bits if there are any. That seems like a lot of work.
>
> Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit > value?
>
>
> Thanks,
> Shawn
>
>



Nov 15 '05 #16
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it wasn't worth posting. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps
it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the 16-bit 65816. In particular, I'm doing this so I can debug (I have the
ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware
(such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my plugins can treat an area of memory as a dedicated "something" such as text display or graphics display, etc. For now, I'm concerned not even with the text display. My end result, is actually more akin to being able to debug my NES games I've been into writing lately. Also I want to be ready with my own debugger for Andre LaMothe's upcoming XGameStation console (don't know whether he'll have a debugger or not) but I want a debugger that allows me to change the code while it's being debugged and my simulator allows for it (as long as the source code file is available -- else if it was loading as a .bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable) assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast. I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm primarly a VB.NET programmer) but didn't feel like doing it in Managed C++ but also wanted it to target the .NET framework (as much as is reasonable and I'll find out what that really means when its mature enough to start
using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the 16-bit processor as I haven't deciphered the specs for the CPU yet (the
8-bit CPU is very easy compared to the 16-bit version of the processor). I ultimately will make this Interfacable so I can write a Z80 core, as well.
These bit manipulations are the utmost inner-loop so I have to make them
tight. Looking at the JIT'd code for these methods seems to be decent but I have to make too many function calls (read, jumps) for my tastes but doesn't seem to be hurting me at the moment). Since I can't inline in .NET, I'll have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate 8-bit and 16-bit shifts and rotates. I'm also trying to work out a better way for simulating memory manipulation. Now I'm creating a string of (64kb in length) but manipulating each byte by substrings has got to be expensive. I'm looking into whether a byte array would be better.
Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string of 1's and 0's). I know now have to read the binary back to a numeric, and allow a way to get/set/toggle individual bits.
Thanks,
Shawn
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> So, you're writing an emulator for which processor??
>
> --
> Bob Powell [MVP]
> C#, System.Drawing
>
> September's edition of Well Formed is now available.
> http://www.bobpowell.net/currentissue.htm
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/gdiplus_faq.htm
>
>
>
> "Shawn B." <le****@html.com> wrote in message
> news:O9*************@TK2MSFTNGP11.phx.gbl...
> > Greetings,
> >
> > I am simulating an assembly language bit rotation in C# and it works
> > wonderfully
> >
> > ---------
> > ...
> > public uint Value;
> > ...
> > public uint RotateRight(byte count) {
> > Value = ((Value >> count) | (Value << (32-count)));
> > return Value;
> > }
> > ---------
> >
> > as long is the value is 32-bit or 64-bit (because of a limitation of the > > shift operator). I need it to work on an 8-bit or 16-bit value, as
well.
> I
> > presume I'd have to have knowledge of the intended size of the value and > do
> > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and > clear
> > the extra bits if there are any. That seems like a lot of work.
> >
> > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit > > value?
> >
> >
> > Thanks,
> > Shawn
> >
> >
>
>



Nov 15 '05 #17
Oh, nice classes by the way. They seem pretty fast.
:)

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps
it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
Don't miss a thing, d'ya? =))

I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the 16-bit 65816. In particular, I'm doing this so I can debug (I have the
ability to modify the assembly language while the debugger is running
(similar to VB6 and VC++ Edit-and-Continue). I can only see raw
instructions. I'm not designing it to be a particular piece of hardware
(such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my plugins can treat an area of memory as a dedicated "something" such as text display or graphics display, etc. For now, I'm concerned not even with the text display. My end result, is actually more akin to being able to debug my NES games I've been into writing lately. Also I want to be ready with my own debugger for Andre LaMothe's upcoming XGameStation console (don't know whether he'll have a debugger or not) but I want a debugger that allows me to change the code while it's being debugged and my simulator allows for it (as long as the source code file is available -- else if it was loading as a .bin file then it won't be editable during a debug session).

So it's really more than a simulator. It's an integrated (non-seperable) assembly, linker, dissassembler, debugger, simulator, etc. kind of a beast. I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm primarly a VB.NET programmer) but didn't feel like doing it in Managed C++ but also wanted it to target the .NET framework (as much as is reasonable and I'll find out what that really means when its mature enough to start
using reliably).

I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the 16-bit processor as I haven't deciphered the specs for the CPU yet (the
8-bit CPU is very easy compared to the 16-bit version of the processor). I ultimately will make this Interfacable so I can write a Z80 core, as well.
These bit manipulations are the utmost inner-loop so I have to make them
tight. Looking at the JIT'd code for these methods seems to be decent but I have to make too many function calls (read, jumps) for my tastes but doesn't seem to be hurting me at the moment). Since I can't inline in .NET, I'll have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
generate the best code for the rotates and shifts because I have to emulate 8-bit and 16-bit shifts and rotates. I'm also trying to work out a better way for simulating memory manipulation. Now I'm creating a string of (64kb in length) but manipulating each byte by substrings has got to be expensive. I'm looking into whether a byte array would be better.
Thanks for the help,
if interested, I'll post my binary class here for all to see.

I've completed the Rotates, Shift, converting numerics into binary (string of 1's and 0's). I know now have to read the binary back to a numeric, and allow a way to get/set/toggle individual bits.
Thanks,
Shawn
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> So, you're writing an emulator for which processor??
>
> --
> Bob Powell [MVP]
> C#, System.Drawing
>
> September's edition of Well Formed is now available.
> http://www.bobpowell.net/currentissue.htm
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/gdiplus_faq.htm
>
>
>
> "Shawn B." <le****@html.com> wrote in message
> news:O9*************@TK2MSFTNGP11.phx.gbl...
> > Greetings,
> >
> > I am simulating an assembly language bit rotation in C# and it works
> > wonderfully
> >
> > ---------
> > ...
> > public uint Value;
> > ...
> > public uint RotateRight(byte count) {
> > Value = ((Value >> count) | (Value << (32-count)));
> > return Value;
> > }
> > ---------
> >
> > as long is the value is 32-bit or 64-bit (because of a limitation of the > > shift operator). I need it to work on an 8-bit or 16-bit value, as
well.
> I
> > presume I'd have to have knowledge of the intended size of the value and > do
> > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and > clear
> > the extra bits if there are any. That seems like a lot of work.
> >
> > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit > > value?
> >
> >
> > Thanks,
> > Shawn
> >
> >
>
>



Nov 15 '05 #18
Robert,

You're first suggestion crossed my mind, I have already started eliminating
the temp variables thus far (thus far only in the 8-bit operator overloads
region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...

I also plan to move the Shift function logic into the overload to avoid the
extra function call.

The GetBit optimization hint you provided is what I've been wanting to do
but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like
it's necessary. Perhaps I could just make the if based on some calculation
and then just throw an exception at the end of the function and avoid that
early check, perhaps that would be a better approach. Such as (if (Value ==
0 && Value < SIZE-1) {)

There is one thing I need help on.
I don't like the GetBit, SetBit approach. I would rather be able to say x =
BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x =
x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a
reference of the Binary class into it so it would be able to change the bit
if a bit was set. Not a good approach. Do you have any ideas?
Thanks for your help,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it wasn't worth posting. :)

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
> Don't miss a thing, d'ya? =))
>
> I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and
the
> 16-bit 65816. In particular, I'm doing this so I can debug (I have
the > ability to modify the assembly language while the debugger is running > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> instructions. I'm not designing it to be a particular piece of hardware > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> plugins can treat an area of memory as a dedicated "something" such
as text
> display or graphics display, etc. For now, I'm concerned not even
with the
> text display. My end result, is actually more akin to being able to

debug
> my NES games I've been into writing lately. Also I want to be ready

with my
> own debugger for Andre LaMothe's upcoming XGameStation console
(don't know
> whether he'll have a debugger or not) but I want a debugger that
allows me
> to change the code while it's being debugged and my simulator allows
for it
> (as long as the source code file is available -- else if it was
loading as a
> .bin file then it won't be editable during a debug session).
>
> So it's really more than a simulator. It's an integrated

(non-seperable)
> assembly, linker, dissassembler, debugger, simulator, etc. kind of a

beast.
> I chose C# because it's not the kind of thing I'd want to do in
VB.NET (I'm
> primarly a VB.NET programmer) but didn't feel like doing it in
Managed C++
> but also wanted it to target the .NET framework (as much as is

reasonable
> and I'll find out what that really means when its mature enough to
start > using reliably).
>
> I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> 16-bit processor as I haven't deciphered the specs for the CPU yet
(the > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> ultimately will make this Interfacable so I can write a Z80 core, as

well.
>
> These bit manipulations are the utmost inner-loop so I have to make
them > tight. Looking at the JIT'd code for these methods seems to be decent but I
> have to make too many function calls (read, jumps) for my tastes but

doesn't
> seem to be hurting me at the moment). Since I can't inline in .NET,

I'll
> have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> generate the best code for the rotates and shifts because I have to

emulate
> 8-bit and 16-bit shifts and rotates. I'm also trying to work out a

better
> way for simulating memory manipulation. Now I'm creating a string
of (64kb
> in length) but manipulating each byte by substrings has got to be

expensive.
> I'm looking into whether a byte array would be better.
>
>
> Thanks for the help,
> if interested, I'll post my binary class here for all to see.
>
> I've completed the Rotates, Shift, converting numerics into binary

(string
> of 1's and 0's). I know now have to read the binary back to a
numeric, and
> allow a way to get/set/toggle individual bits.
>
>
> Thanks,
> Shawn
>
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > So, you're writing an emulator for which processor??
> >
> > --
> > Bob Powell [MVP]
> > C#, System.Drawing
> >
> > September's edition of Well Formed is now available.
> > http://www.bobpowell.net/currentissue.htm
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdiplus_faq.htm
> >
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > Greetings,
> > >
> > > I am simulating an assembly language bit rotation in C# and it
works > > > wonderfully
> > >
> > > ---------
> > > ...
> > > public uint Value;
> > > ...
> > > public uint RotateRight(byte count) {
> > > Value = ((Value >> count) | (Value << (32-count)));
> > > return Value;
> > > }
> > > ---------
> > >
> > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > shift operator). I need it to work on an 8-bit or 16-bit value,
as > well.
> > I
> > > presume I'd have to have knowledge of the intended size of the

value and
> > do
> > > the shift and move the bit and the 0 or 7, or 0 or 15 bit
position and
> > clear
> > > the extra bits if there are any. That seems like a lot of work.
> > >
> > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit
or 64-bit
> > > value?
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> >
> >
>
>



Nov 15 '05 #19
Also,

I'm using byte in some of the parameters such as GetBit(byte index), I'm
wondering, if there incurrs a performance penalty because the processor
thinks in 32-bit and not 8-bit, perhaps I should just make that int? Do you
think it matters? I know in assembly language it takes longer to "think"
8-bit than 32-bit (I program windows in asm as a hobby).
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it wasn't worth posting. :)

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
> Don't miss a thing, d'ya? =))
>
> I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and
the
> 16-bit 65816. In particular, I'm doing this so I can debug (I have
the > ability to modify the assembly language while the debugger is running > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> instructions. I'm not designing it to be a particular piece of hardware > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> plugins can treat an area of memory as a dedicated "something" such
as text
> display or graphics display, etc. For now, I'm concerned not even
with the
> text display. My end result, is actually more akin to being able to

debug
> my NES games I've been into writing lately. Also I want to be ready

with my
> own debugger for Andre LaMothe's upcoming XGameStation console
(don't know
> whether he'll have a debugger or not) but I want a debugger that
allows me
> to change the code while it's being debugged and my simulator allows
for it
> (as long as the source code file is available -- else if it was
loading as a
> .bin file then it won't be editable during a debug session).
>
> So it's really more than a simulator. It's an integrated

(non-seperable)
> assembly, linker, dissassembler, debugger, simulator, etc. kind of a

beast.
> I chose C# because it's not the kind of thing I'd want to do in
VB.NET (I'm
> primarly a VB.NET programmer) but didn't feel like doing it in
Managed C++
> but also wanted it to target the .NET framework (as much as is

reasonable
> and I'll find out what that really means when its mature enough to
start > using reliably).
>
> I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> 16-bit processor as I haven't deciphered the specs for the CPU yet
(the > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> ultimately will make this Interfacable so I can write a Z80 core, as

well.
>
> These bit manipulations are the utmost inner-loop so I have to make
them > tight. Looking at the JIT'd code for these methods seems to be decent but I
> have to make too many function calls (read, jumps) for my tastes but

doesn't
> seem to be hurting me at the moment). Since I can't inline in .NET,

I'll
> have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> generate the best code for the rotates and shifts because I have to

emulate
> 8-bit and 16-bit shifts and rotates. I'm also trying to work out a

better
> way for simulating memory manipulation. Now I'm creating a string
of (64kb
> in length) but manipulating each byte by substrings has got to be

expensive.
> I'm looking into whether a byte array would be better.
>
>
> Thanks for the help,
> if interested, I'll post my binary class here for all to see.
>
> I've completed the Rotates, Shift, converting numerics into binary

(string
> of 1's and 0's). I know now have to read the binary back to a
numeric, and
> allow a way to get/set/toggle individual bits.
>
>
> Thanks,
> Shawn
>
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > So, you're writing an emulator for which processor??
> >
> > --
> > Bob Powell [MVP]
> > C#, System.Drawing
> >
> > September's edition of Well Formed is now available.
> > http://www.bobpowell.net/currentissue.htm
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdiplus_faq.htm
> >
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > Greetings,
> > >
> > > I am simulating an assembly language bit rotation in C# and it
works > > > wonderfully
> > >
> > > ---------
> > > ...
> > > public uint Value;
> > > ...
> > > public uint RotateRight(byte count) {
> > > Value = ((Value >> count) | (Value << (32-count)));
> > > return Value;
> > > }
> > > ---------
> > >
> > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > shift operator). I need it to work on an 8-bit or 16-bit value,
as > well.
> > I
> > > presume I'd have to have knowledge of the intended size of the

value and
> > do
> > > the shift and move the bit and the 0 or 7, or 0 or 15 bit
position and
> > clear
> > > the extra bits if there are any. That seems like a lot of work.
> > >
> > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit
or 64-bit
> > > value?
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> >
> >
>
>



Nov 15 '05 #20
Robert,

I updated the objects with the opimizations you suggested and a few others I
thought were neccessary. I also added an "And" and "Or" method to the
classes. I changed the Rotate and Shift methods to not set Value and then
return Value. Now they only return it. The problem was if I said "bin2 =
bin.RotateLeft(2);" both bin2 and bin would end up with a new value. I
originally did that because before I was overloading the operators I was
actually just saying "bin.RotateLeft(2);" then later decided to make it
return the new value, as well. Now it is not necessary with all the
overloads. Not very consistant with the rest of the .NET framework so I
corrected it. I put a special thanks in at the end of the ReadMe.

Okay, now, let me know if you have any other ideas or optimization tips =)).
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist."); }
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it wasn't worth posting. :)

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
Do post it here!
I'd be interested to see, as I'm sure many others would.
You'll probably get a ton of optimization hints from it. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:OW**************@tk2msftngp13.phx.gbl...
> Don't miss a thing, d'ya? =))
>
> I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and
the
> 16-bit 65816. In particular, I'm doing this so I can debug (I have
the > ability to modify the assembly language while the debugger is running > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> instructions. I'm not designing it to be a particular piece of hardware > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> plugins can treat an area of memory as a dedicated "something" such
as text
> display or graphics display, etc. For now, I'm concerned not even
with the
> text display. My end result, is actually more akin to being able to

debug
> my NES games I've been into writing lately. Also I want to be ready

with my
> own debugger for Andre LaMothe's upcoming XGameStation console
(don't know
> whether he'll have a debugger or not) but I want a debugger that
allows me
> to change the code while it's being debugged and my simulator allows
for it
> (as long as the source code file is available -- else if it was
loading as a
> .bin file then it won't be editable during a debug session).
>
> So it's really more than a simulator. It's an integrated

(non-seperable)
> assembly, linker, dissassembler, debugger, simulator, etc. kind of a

beast.
> I chose C# because it's not the kind of thing I'd want to do in
VB.NET (I'm
> primarly a VB.NET programmer) but didn't feel like doing it in
Managed C++
> but also wanted it to target the .NET framework (as much as is

reasonable
> and I'll find out what that really means when its mature enough to
start > using reliably).
>
> I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> 16-bit processor as I haven't deciphered the specs for the CPU yet
(the > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> ultimately will make this Interfacable so I can write a Z80 core, as

well.
>
> These bit manipulations are the utmost inner-loop so I have to make
them > tight. Looking at the JIT'd code for these methods seems to be decent but I
> have to make too many function calls (read, jumps) for my tastes but

doesn't
> seem to be hurting me at the moment). Since I can't inline in .NET,

I'll
> have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> generate the best code for the rotates and shifts because I have to

emulate
> 8-bit and 16-bit shifts and rotates. I'm also trying to work out a

better
> way for simulating memory manipulation. Now I'm creating a string
of (64kb
> in length) but manipulating each byte by substrings has got to be

expensive.
> I'm looking into whether a byte array would be better.
>
>
> Thanks for the help,
> if interested, I'll post my binary class here for all to see.
>
> I've completed the Rotates, Shift, converting numerics into binary

(string
> of 1's and 0's). I know now have to read the binary back to a
numeric, and
> allow a way to get/set/toggle individual bits.
>
>
> Thanks,
> Shawn
>
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > So, you're writing an emulator for which processor??
> >
> > --
> > Bob Powell [MVP]
> > C#, System.Drawing
> >
> > September's edition of Well Formed is now available.
> > http://www.bobpowell.net/currentissue.htm
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdiplus_faq.htm
> >
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > Greetings,
> > >
> > > I am simulating an assembly language bit rotation in C# and it
works > > > wonderfully
> > >
> > > ---------
> > > ...
> > > public uint Value;
> > > ...
> > > public uint RotateRight(byte count) {
> > > Value = ((Value >> count) | (Value << (32-count)));
> > > return Value;
> > > }
> > > ---------
> > >
> > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > shift operator). I need it to work on an 8-bit or 16-bit value,
as > well.
> > I
> > > presume I'd have to have knowledge of the intended size of the

value and
> > do
> > > the shift and move the bit and the 0 or 7, or 0 or 15 bit
position and
> > clear
> > > the extra bits if there are any. That seems like a lot of work.
> > >
> > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit
or 64-bit
> > > value?
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> >
> >
>
>



Nov 15 '05 #21
Hi Shawn,

In GetBit, SetBit or in shift operators, you don't need to check of are shifting 0 steps, just do it anyway. It will return the
correct value.
Couldn't you have an indexer on the BinaryXX class itself?
Then you'd say,
BinaryX[0] = 1;
BinaryX[2] = BinaryX[0];

For me, that would make more sense than a separate Bit class.

public int this [int index] // Indexer declaration
{
get
{
//GetBit here
}
set
{
//SetBit here
}
}
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Robert,

You're first suggestion crossed my mind, I have already started eliminating
the temp variables thus far (thus far only in the 8-bit operator overloads
region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...

I also plan to move the Shift function logic into the overload to avoid the
extra function call.

The GetBit optimization hint you provided is what I've been wanting to do
but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like
it's necessary. Perhaps I could just make the if based on some calculation
and then just throw an exception at the end of the function and avoid that
early check, perhaps that would be a better approach. Such as (if (Value ==
0 && Value < SIZE-1) {)

There is one thing I need help on.
I don't like the GetBit, SetBit approach. I would rather be able to say x =
BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x =
x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a
reference of the Binary class into it so it would be able to change the bit
if a bit was set. Not a good approach. Do you have any ideas?
Thanks for your help,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
> Do post it here!
> I'd be interested to see, as I'm sure many others would.
> You'll probably get a ton of optimization hints from it. :)
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <le****@html.com> wrote in message
news:OW**************@tk2msftngp13.phx.gbl...
> > Don't miss a thing, d'ya? =))
> >
> > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
> > 16-bit 65816. In particular, I'm doing this so I can debug (I have the > > ability to modify the assembly language while the debugger is running > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > instructions. I'm not designing it to be a particular piece of hardware > > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> > plugins can treat an area of memory as a dedicated "something" such as text
> > display or graphics display, etc. For now, I'm concerned not even with the
> > text display. My end result, is actually more akin to being able to
debug
> > my NES games I've been into writing lately. Also I want to be ready
with my
> > own debugger for Andre LaMothe's upcoming XGameStation console (don't know
> > whether he'll have a debugger or not) but I want a debugger that allows me
> > to change the code while it's being debugged and my simulator allows for it
> > (as long as the source code file is available -- else if it was loading as a
> > .bin file then it won't be editable during a debug session).
> >
> > So it's really more than a simulator. It's an integrated
(non-seperable)
> > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
beast.
> > I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm
> > primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
> > but also wanted it to target the .NET framework (as much as is
reasonable
> > and I'll find out what that really means when its mature enough to start > > using reliably).
> >
> > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> > 16-bit processor as I haven't deciphered the specs for the CPU yet (the > > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> > ultimately will make this Interfacable so I can write a Z80 core, as
well.
> >
> > These bit manipulations are the utmost inner-loop so I have to make them > > tight. Looking at the JIT'd code for these methods seems to be decent but I
> > have to make too many function calls (read, jumps) for my tastes but
doesn't
> > seem to be hurting me at the moment). Since I can't inline in .NET,
I'll
> > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > generate the best code for the rotates and shifts because I have to
emulate
> > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
better
> > way for simulating memory manipulation. Now I'm creating a string of (64kb
> > in length) but manipulating each byte by substrings has got to be
expensive.
> > I'm looking into whether a byte array would be better.
> >
> >
> > Thanks for the help,
> > if interested, I'll post my binary class here for all to see.
> >
> > I've completed the Rotates, Shift, converting numerics into binary
(string
> > of 1's and 0's). I know now have to read the binary back to a numeric, and
> > allow a way to get/set/toggle individual bits.
> >
> >
> > Thanks,
> > Shawn
> >
> >
> > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > news:%2****************@tk2msftngp13.phx.gbl...
> > > So, you're writing an emulator for which processor??
> > >
> > > --
> > > Bob Powell [MVP]
> > > C#, System.Drawing
> > >
> > > September's edition of Well Formed is now available.
> > > http://www.bobpowell.net/currentissue.htm
> > >
> > > Answer those GDI+ questions with the GDI+ FAQ
> > > http://www.bobpowell.net/gdiplus_faq.htm
> > >
> > >
> > >
> > > "Shawn B." <le****@html.com> wrote in message
> > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > Greetings,
> > > >
> > > > I am simulating an assembly language bit rotation in C# and it works > > > > wonderfully
> > > >
> > > > ---------
> > > > ...
> > > > public uint Value;
> > > > ...
> > > > public uint RotateRight(byte count) {
> > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > return Value;
> > > > }
> > > > ---------
> > > >
> > > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > > shift operator). I need it to work on an 8-bit or 16-bit value, as > > well.
> > > I
> > > > presume I'd have to have knowledge of the intended size of the value and
> > > do
> > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and
> > > clear
> > > > the extra bits if there are any. That seems like a lot of work.
> > > >
> > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> > > > value?
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #22
I think in index parameters and such, you'd probably be better off using an int,
as int is sort of the default. If you use byte, an implicit conversion occurs.

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:u5**************@tk2msftngp13.phx.gbl...
Also,

I'm using byte in some of the parameters such as GetBit(byte index), I'm
wondering, if there incurrs a performance penalty because the processor
thinks in 32-bit and not 8-bit, perhaps I should just make that int? Do you
think it matters? I know in assembly language it takes longer to "think"
8-bit than 32-bit (I program windows in asm as a hobby).
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
> Do post it here!
> I'd be interested to see, as I'm sure many others would.
> You'll probably get a ton of optimization hints from it. :)
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <le****@html.com> wrote in message
news:OW**************@tk2msftngp13.phx.gbl...
> > Don't miss a thing, d'ya? =))
> >
> > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
> > 16-bit 65816. In particular, I'm doing this so I can debug (I have the > > ability to modify the assembly language while the debugger is running > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > instructions. I'm not designing it to be a particular piece of hardware > > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> > plugins can treat an area of memory as a dedicated "something" such as text
> > display or graphics display, etc. For now, I'm concerned not even with the
> > text display. My end result, is actually more akin to being able to
debug
> > my NES games I've been into writing lately. Also I want to be ready
with my
> > own debugger for Andre LaMothe's upcoming XGameStation console (don't know
> > whether he'll have a debugger or not) but I want a debugger that allows me
> > to change the code while it's being debugged and my simulator allows for it
> > (as long as the source code file is available -- else if it was loading as a
> > .bin file then it won't be editable during a debug session).
> >
> > So it's really more than a simulator. It's an integrated
(non-seperable)
> > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
beast.
> > I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm
> > primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
> > but also wanted it to target the .NET framework (as much as is
reasonable
> > and I'll find out what that really means when its mature enough to start > > using reliably).
> >
> > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> > 16-bit processor as I haven't deciphered the specs for the CPU yet (the > > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> > ultimately will make this Interfacable so I can write a Z80 core, as
well.
> >
> > These bit manipulations are the utmost inner-loop so I have to make them > > tight. Looking at the JIT'd code for these methods seems to be decent but I
> > have to make too many function calls (read, jumps) for my tastes but
doesn't
> > seem to be hurting me at the moment). Since I can't inline in .NET,
I'll
> > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > generate the best code for the rotates and shifts because I have to
emulate
> > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
better
> > way for simulating memory manipulation. Now I'm creating a string of (64kb
> > in length) but manipulating each byte by substrings has got to be
expensive.
> > I'm looking into whether a byte array would be better.
> >
> >
> > Thanks for the help,
> > if interested, I'll post my binary class here for all to see.
> >
> > I've completed the Rotates, Shift, converting numerics into binary
(string
> > of 1's and 0's). I know now have to read the binary back to a numeric, and
> > allow a way to get/set/toggle individual bits.
> >
> >
> > Thanks,
> > Shawn
> >
> >
> > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > news:%2****************@tk2msftngp13.phx.gbl...
> > > So, you're writing an emulator for which processor??
> > >
> > > --
> > > Bob Powell [MVP]
> > > C#, System.Drawing
> > >
> > > September's edition of Well Formed is now available.
> > > http://www.bobpowell.net/currentissue.htm
> > >
> > > Answer those GDI+ questions with the GDI+ FAQ
> > > http://www.bobpowell.net/gdiplus_faq.htm
> > >
> > >
> > >
> > > "Shawn B." <le****@html.com> wrote in message
> > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > Greetings,
> > > >
> > > > I am simulating an assembly language bit rotation in C# and it works > > > > wonderfully
> > > >
> > > > ---------
> > > > ...
> > > > public uint Value;
> > > > ...
> > > > public uint RotateRight(byte count) {
> > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > return Value;
> > > > }
> > > > ---------
> > > >
> > > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > > shift operator). I need it to work on an 8-bit or 16-bit value, as > > well.
> > > I
> > > > presume I'd have to have knowledge of the intended size of the value and
> > > do
> > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and
> > > clear
> > > > the extra bits if there are any. That seems like a lot of work.
> > > >
> > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> > > > value?
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #23
Yes, it's better to just return the value in those cases.
I'm gonna go check the new code now. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:u9**************@TK2MSFTNGP12.phx.gbl...
Robert,

I updated the objects with the opimizations you suggested and a few others I
thought were neccessary. I also added an "And" and "Or" method to the
classes. I changed the Rotate and Shift methods to not set Value and then
return Value. Now they only return it. The problem was if I said "bin2 =
bin.RotateLeft(2);" both bin2 and bin would end up with a new value. I
originally did that because before I was overloading the operators I was
actually just saying "bin.RotateLeft(2);" then later decided to make it
return the new value, as well. Now it is not necessary with all the
overloads. Not very consistant with the rest of the .NET framework so I
corrected it. I put a special thanks in at the end of the ReadMe.

Okay, now, let me know if you have any other ideas or optimization tips =)).
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
> Do post it here!
> I'd be interested to see, as I'm sure many others would.
> You'll probably get a ton of optimization hints from it. :)
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <le****@html.com> wrote in message
news:OW**************@tk2msftngp13.phx.gbl...
> > Don't miss a thing, d'ya? =))
> >
> > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
> > 16-bit 65816. In particular, I'm doing this so I can debug (I have the > > ability to modify the assembly language while the debugger is running > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > instructions. I'm not designing it to be a particular piece of hardware > > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> > plugins can treat an area of memory as a dedicated "something" such as text
> > display or graphics display, etc. For now, I'm concerned not even with the
> > text display. My end result, is actually more akin to being able to
debug
> > my NES games I've been into writing lately. Also I want to be ready
with my
> > own debugger for Andre LaMothe's upcoming XGameStation console (don't know
> > whether he'll have a debugger or not) but I want a debugger that allows me
> > to change the code while it's being debugged and my simulator allows for it
> > (as long as the source code file is available -- else if it was loading as a
> > .bin file then it won't be editable during a debug session).
> >
> > So it's really more than a simulator. It's an integrated
(non-seperable)
> > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
beast.
> > I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm
> > primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
> > but also wanted it to target the .NET framework (as much as is
reasonable
> > and I'll find out what that really means when its mature enough to start > > using reliably).
> >
> > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> > 16-bit processor as I haven't deciphered the specs for the CPU yet (the > > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> > ultimately will make this Interfacable so I can write a Z80 core, as
well.
> >
> > These bit manipulations are the utmost inner-loop so I have to make them > > tight. Looking at the JIT'd code for these methods seems to be decent but I
> > have to make too many function calls (read, jumps) for my tastes but
doesn't
> > seem to be hurting me at the moment). Since I can't inline in .NET,
I'll
> > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > generate the best code for the rotates and shifts because I have to
emulate
> > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
better
> > way for simulating memory manipulation. Now I'm creating a string of (64kb
> > in length) but manipulating each byte by substrings has got to be
expensive.
> > I'm looking into whether a byte array would be better.
> >
> >
> > Thanks for the help,
> > if interested, I'll post my binary class here for all to see.
> >
> > I've completed the Rotates, Shift, converting numerics into binary
(string
> > of 1's and 0's). I know now have to read the binary back to a numeric, and
> > allow a way to get/set/toggle individual bits.
> >
> >
> > Thanks,
> > Shawn
> >
> >
> > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > news:%2****************@tk2msftngp13.phx.gbl...
> > > So, you're writing an emulator for which processor??
> > >
> > > --
> > > Bob Powell [MVP]
> > > C#, System.Drawing
> > >
> > > September's edition of Well Formed is now available.
> > > http://www.bobpowell.net/currentissue.htm
> > >
> > > Answer those GDI+ questions with the GDI+ FAQ
> > > http://www.bobpowell.net/gdiplus_faq.htm
> > >
> > >
> > >
> > > "Shawn B." <le****@html.com> wrote in message
> > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > Greetings,
> > > >
> > > > I am simulating an assembly language bit rotation in C# and it works > > > > wonderfully
> > > >
> > > > ---------
> > > > ...
> > > > public uint Value;
> > > > ...
> > > > public uint RotateRight(byte count) {
> > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > return Value;
> > > > }
> > > > ---------
> > > >
> > > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > > shift operator). I need it to work on an 8-bit or 16-bit value, as > > well.
> > > I
> > > > presume I'd have to have knowledge of the intended size of the value and
> > > do
> > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and
> > > clear
> > > > the extra bits if there are any. That seems like a lot of work.
> > > >
> > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> > > > value?
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #24
Another suggestion..
You have a method called IsBinary to determine whether a string is valid and can be parsed.
In the framework, methods called IsXXX and return a bool usually won't throw an exception (which you method does).
The purpose of these methods (as far as I know) is to avoid catching errors by Exception handling, cause it incurrs such a
performance penalty. Remove the exception from this method and return false if the string is too long to be compliant.

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Robert,

You're first suggestion crossed my mind, I have already started eliminating
the temp variables thus far (thus far only in the 8-bit operator overloads
region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...

I also plan to move the Shift function logic into the overload to avoid the
extra function call.

The GetBit optimization hint you provided is what I've been wanting to do
but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like
it's necessary. Perhaps I could just make the if based on some calculation
and then just throw an exception at the end of the function and avoid that
early check, perhaps that would be a better approach. Such as (if (Value ==
0 && Value < SIZE-1) {)

There is one thing I need help on.
I don't like the GetBit, SetBit approach. I would rather be able to say x =
BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x =
x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a
reference of the Binary class into it so it would be able to change the bit
if a bit was set. Not a good approach. Do you have any ideas?
Thanks for your help,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
> Do post it here!
> I'd be interested to see, as I'm sure many others would.
> You'll probably get a ton of optimization hints from it. :)
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <le****@html.com> wrote in message
news:OW**************@tk2msftngp13.phx.gbl...
> > Don't miss a thing, d'ya? =))
> >
> > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
> > 16-bit 65816. In particular, I'm doing this so I can debug (I have the > > ability to modify the assembly language while the debugger is running > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > instructions. I'm not designing it to be a particular piece of hardware > > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> > plugins can treat an area of memory as a dedicated "something" such as text
> > display or graphics display, etc. For now, I'm concerned not even with the
> > text display. My end result, is actually more akin to being able to
debug
> > my NES games I've been into writing lately. Also I want to be ready
with my
> > own debugger for Andre LaMothe's upcoming XGameStation console (don't know
> > whether he'll have a debugger or not) but I want a debugger that allows me
> > to change the code while it's being debugged and my simulator allows for it
> > (as long as the source code file is available -- else if it was loading as a
> > .bin file then it won't be editable during a debug session).
> >
> > So it's really more than a simulator. It's an integrated
(non-seperable)
> > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
beast.
> > I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm
> > primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
> > but also wanted it to target the .NET framework (as much as is
reasonable
> > and I'll find out what that really means when its mature enough to start > > using reliably).
> >
> > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> > 16-bit processor as I haven't deciphered the specs for the CPU yet (the > > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> > ultimately will make this Interfacable so I can write a Z80 core, as
well.
> >
> > These bit manipulations are the utmost inner-loop so I have to make them > > tight. Looking at the JIT'd code for these methods seems to be decent but I
> > have to make too many function calls (read, jumps) for my tastes but
doesn't
> > seem to be hurting me at the moment). Since I can't inline in .NET,
I'll
> > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > generate the best code for the rotates and shifts because I have to
emulate
> > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
better
> > way for simulating memory manipulation. Now I'm creating a string of (64kb
> > in length) but manipulating each byte by substrings has got to be
expensive.
> > I'm looking into whether a byte array would be better.
> >
> >
> > Thanks for the help,
> > if interested, I'll post my binary class here for all to see.
> >
> > I've completed the Rotates, Shift, converting numerics into binary
(string
> > of 1's and 0's). I know now have to read the binary back to a numeric, and
> > allow a way to get/set/toggle individual bits.
> >
> >
> > Thanks,
> > Shawn
> >
> >
> > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > news:%2****************@tk2msftngp13.phx.gbl...
> > > So, you're writing an emulator for which processor??
> > >
> > > --
> > > Bob Powell [MVP]
> > > C#, System.Drawing
> > >
> > > September's edition of Well Formed is now available.
> > > http://www.bobpowell.net/currentissue.htm
> > >
> > > Answer those GDI+ questions with the GDI+ FAQ
> > > http://www.bobpowell.net/gdiplus_faq.htm
> > >
> > >
> > >
> > > "Shawn B." <le****@html.com> wrote in message
> > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > Greetings,
> > > >
> > > > I am simulating an assembly language bit rotation in C# and it works > > > > wonderfully
> > > >
> > > > ---------
> > > > ...
> > > > public uint Value;
> > > > ...
> > > > public uint RotateRight(byte count) {
> > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > return Value;
> > > > }
> > > > ---------
> > > >
> > > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > > shift operator). I need it to work on an 8-bit or 16-bit value, as > > well.
> > > I
> > > > presume I'd have to have knowledge of the intended size of the value and
> > > do
> > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and
> > > clear
> > > > the extra bits if there are any. That seems like a lot of work.
> > > >
> > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> > > > value?
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #25
Removing the if check in SetBit:
Value = (ushort)((~(0x01 << index))&Value|(0x01 << index));

As always, untested. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Robert,

You're first suggestion crossed my mind, I have already started eliminating
the temp variables thus far (thus far only in the 8-bit operator overloads
region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...

I also plan to move the Shift function logic into the overload to avoid the
extra function call.

The GetBit optimization hint you provided is what I've been wanting to do
but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like
it's necessary. Perhaps I could just make the if based on some calculation
and then just throw an exception at the end of the function and avoid that
early check, perhaps that would be a better approach. Such as (if (Value ==
0 && Value < SIZE-1) {)

There is one thing I need help on.
I don't like the GetBit, SetBit approach. I would rather be able to say x =
BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x =
x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a
reference of the Binary class into it so it would be able to change the bit
if a bit was set. Not a good approach. Do you have any ideas?
Thanks for your help,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "

does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
Well, I've posted it twice and no one is jumpting to optimize it... perhaps it's just so well working that there's no need? he he


Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:Oi**************@TK2MSFTNGP09.phx.gbl...
> Do post it here!
> I'd be interested to see, as I'm sure many others would.
> You'll probably get a ton of optimization hints from it. :)
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <le****@html.com> wrote in message
news:OW**************@tk2msftngp13.phx.gbl...
> > Don't miss a thing, d'ya? =))
> >
> > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and the
> > 16-bit 65816. In particular, I'm doing this so I can debug (I have the > > ability to modify the assembly language while the debugger is running > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > instructions. I'm not designing it to be a particular piece of hardware > > (such as the Apple, Atari, Commadore, etc.). I can supply it plugins so my
> > plugins can treat an area of memory as a dedicated "something" such as text
> > display or graphics display, etc. For now, I'm concerned not even with the
> > text display. My end result, is actually more akin to being able to
debug
> > my NES games I've been into writing lately. Also I want to be ready
with my
> > own debugger for Andre LaMothe's upcoming XGameStation console (don't know
> > whether he'll have a debugger or not) but I want a debugger that allows me
> > to change the code while it's being debugged and my simulator allows for it
> > (as long as the source code file is available -- else if it was loading as a
> > .bin file then it won't be editable during a debug session).
> >
> > So it's really more than a simulator. It's an integrated
(non-seperable)
> > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
beast.
> > I chose C# because it's not the kind of thing I'd want to do in VB.NET (I'm
> > primarly a VB.NET programmer) but didn't feel like doing it in Managed C++
> > but also wanted it to target the .NET framework (as much as is
reasonable
> > and I'll find out what that really means when its mature enough to start > > using reliably).
> >
> > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do emulating the
> > 16-bit processor as I haven't deciphered the specs for the CPU yet (the > > 8-bit CPU is very easy compared to the 16-bit version of the processor). I
> > ultimately will make this Interfacable so I can write a Z80 core, as
well.
> >
> > These bit manipulations are the utmost inner-loop so I have to make them > > tight. Looking at the JIT'd code for these methods seems to be decent but I
> > have to make too many function calls (read, jumps) for my tastes but
doesn't
> > seem to be hurting me at the moment). Since I can't inline in .NET,
I'll
> > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't
> > generate the best code for the rotates and shifts because I have to
emulate
> > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a
better
> > way for simulating memory manipulation. Now I'm creating a string of (64kb
> > in length) but manipulating each byte by substrings has got to be
expensive.
> > I'm looking into whether a byte array would be better.
> >
> >
> > Thanks for the help,
> > if interested, I'll post my binary class here for all to see.
> >
> > I've completed the Rotates, Shift, converting numerics into binary
(string
> > of 1's and 0's). I know now have to read the binary back to a numeric, and
> > allow a way to get/set/toggle individual bits.
> >
> >
> > Thanks,
> > Shawn
> >
> >
> > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> > news:%2****************@tk2msftngp13.phx.gbl...
> > > So, you're writing an emulator for which processor??
> > >
> > > --
> > > Bob Powell [MVP]
> > > C#, System.Drawing
> > >
> > > September's edition of Well Formed is now available.
> > > http://www.bobpowell.net/currentissue.htm
> > >
> > > Answer those GDI+ questions with the GDI+ FAQ
> > > http://www.bobpowell.net/gdiplus_faq.htm
> > >
> > >
> > >
> > > "Shawn B." <le****@html.com> wrote in message
> > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > Greetings,
> > > >
> > > > I am simulating an assembly language bit rotation in C# and it works > > > > wonderfully
> > > >
> > > > ---------
> > > > ...
> > > > public uint Value;
> > > > ...
> > > > public uint RotateRight(byte count) {
> > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > return Value;
> > > > }
> > > > ---------
> > > >
> > > > as long is the value is 32-bit or 64-bit (because of a limitation of the
> > > > shift operator). I need it to work on an 8-bit or 16-bit value, as > > well.
> > > I
> > > > presume I'd have to have knowledge of the intended size of the value and
> > > do
> > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit position and
> > > clear
> > > > the extra bits if there are any. That seems like a lot of work.
> > > >
> > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or 64-bit
> > > > value?
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #26
Wow, great idea about the indexer, glad I thought of it... =))
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:us**************@TK2MSFTNGP11.phx.gbl...
Hi Shawn,

In GetBit, SetBit or in shift operators, you don't need to check of are shifting 0 steps, just do it anyway. It will return the correct value.
Couldn't you have an indexer on the BinaryXX class itself?
Then you'd say,
BinaryX[0] = 1;
BinaryX[2] = BinaryX[0];

For me, that would make more sense than a separate Bit class.

public int this [int index] // Indexer declaration
{
get
{
//GetBit here
}
set
{
//SetBit here
}
}
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
Robert,

You're first suggestion crossed my mind, I have already started eliminating the temp variables thus far (thus far only in the 8-bit operator overloads region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...
I also plan to move the Shift function logic into the overload to avoid the extra function call.

The GetBit optimization hint you provided is what I've been wanting to do but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like it's necessary. Perhaps I could just make the if based on some calculation and then just throw an exception at the end of the function and avoid that early check, perhaps that would be a better approach. Such as (if (Value == 0 && Value < SIZE-1) {)

There is one thing I need help on.
I don't like the GetBit, SetBit approach. I would rather be able to say x = BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x = x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a reference of the Binary class into it so it would be able to change the bit if a bit was set. Not a good approach. Do you have any ideas?
Thanks for your help,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "
does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
> Well, I've posted it twice and no one is jumpting to optimize it...

perhaps
> it's just so well working that there's no need? he he
>
>
>
>
> Thanks,
> Shawn
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:Oi**************@TK2MSFTNGP09.phx.gbl...
> > Do post it here!
> > I'd be interested to see, as I'm sure many others would.
> > You'll probably get a ton of optimization hints from it. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> news:OW**************@tk2msftngp13.phx.gbl...
> > > Don't miss a thing, d'ya? =))
> > >
> > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit
65c02, and
> the
> > > 16-bit 65816. In particular, I'm doing this so I can debug (I
have the
> > > ability to modify the assembly language while the debugger is

running
> > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > instructions. I'm not designing it to be a particular piece of

hardware
> > > (such as the Apple, Atari, Commadore, etc.). I can supply it

plugins so
> my
> > > plugins can treat an area of memory as a dedicated "something"
such as
> text
> > > display or graphics display, etc. For now, I'm concerned not
even with
> the
> > > text display. My end result, is actually more akin to being
able to > debug
> > > my NES games I've been into writing lately. Also I want to be ready > with my
> > > own debugger for Andre LaMothe's upcoming XGameStation console

(don't
> know
> > > whether he'll have a debugger or not) but I want a debugger that

allows
> me
> > > to change the code while it's being debugged and my simulator allows for
> it
> > > (as long as the source code file is available -- else if it was

loading
> as a
> > > .bin file then it won't be editable during a debug session).
> > >
> > > So it's really more than a simulator. It's an integrated
> (non-seperable)
> > > assembly, linker, dissassembler, debugger, simulator, etc. kind
of a > beast.
> > > I chose C# because it's not the kind of thing I'd want to do in

VB.NET
> (I'm
> > > primarly a VB.NET programmer) but didn't feel like doing it in

Managed
> C++
> > > but also wanted it to target the .NET framework (as much as is
> reasonable
> > > and I'll find out what that really means when its mature enough to start
> > > using reliably).
> > >
> > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz
CPU with
> > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do

emulating
> the
> > > 16-bit processor as I haven't deciphered the specs for the CPU
yet (the
> > > 8-bit CPU is very easy compared to the 16-bit version of the

processor).
> I
> > > ultimately will make this Interfacable so I can write a Z80
core, as > well.
> > >
> > > These bit manipulations are the utmost inner-loop so I have to make them
> > > tight. Looking at the JIT'd code for these methods seems to be

decent
> but I
> > > have to make too many function calls (read, jumps) for my tastes
but > doesn't
> > > seem to be hurting me at the moment). Since I can't inline in ..NET, > I'll
> > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't > > > generate the best code for the rotates and shifts because I have to > emulate
> > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a > better
> > > way for simulating memory manipulation. Now I'm creating a string of
> (64kb
> > > in length) but manipulating each byte by substrings has got to
be > expensive.
> > > I'm looking into whether a byte array would be better.
> > >
> > >
> > > Thanks for the help,
> > > if interested, I'll post my binary class here for all to see.
> > >
> > > I've completed the Rotates, Shift, converting numerics into binary > (string
> > > of 1's and 0's). I know now have to read the binary back to a

numeric,
> and
> > > allow a way to get/set/toggle individual bits.
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > > news:%2****************@tk2msftngp13.phx.gbl...
> > > > So, you're writing an emulator for which processor??
> > > >
> > > > --
> > > > Bob Powell [MVP]
> > > > C#, System.Drawing
> > > >
> > > > September's edition of Well Formed is now available.
> > > > http://www.bobpowell.net/currentissue.htm
> > > >
> > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > >
> > > >
> > > >
> > > > "Shawn B." <le****@html.com> wrote in message
> > > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > > Greetings,
> > > > >
> > > > > I am simulating an assembly language bit rotation in C# and it works
> > > > > wonderfully
> > > > >
> > > > > ---------
> > > > > ...
> > > > > public uint Value;
> > > > > ...
> > > > > public uint RotateRight(byte count) {
> > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > return Value;
> > > > > }
> > > > > ---------
> > > > >
> > > > > as long is the value is 32-bit or 64-bit (because of a

limitation of
> the
> > > > > shift operator). I need it to work on an 8-bit or 16-bit
value, as
> > > well.
> > > > I
> > > > > presume I'd have to have knowledge of the intended size of
the value
> and
> > > > do
> > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit

position
> and
> > > > clear
> > > > > the extra bits if there are any. That seems like a lot of
work. > > > > >
> > > > > Is there a way to simulate an 8 or 16-bit rotate with a

32-bit or
> 64-bit
> > > > > value?
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #27
Already done.
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:eB**************@tk2msftngp13.phx.gbl...
Removing the if check in SetBit:
Value = (ushort)((~(0x01 << index))&Value|(0x01 << index));

As always, untested. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
Robert,

You're first suggestion crossed my mind, I have already started eliminating the temp variables thus far (thus far only in the 8-bit operator overloads region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...
I also plan to move the Shift function logic into the overload to avoid the extra function call.

The GetBit optimization hint you provided is what I've been wanting to do but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like it's necessary. Perhaps I could just make the if based on some calculation and then just throw an exception at the end of the function and avoid that early check, perhaps that would be a better approach. Such as (if (Value == 0 && Value < SIZE-1) {)

There is one thing I need help on.
I don't like the GetBit, SetBit approach. I would rather be able to say x = BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x = x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a reference of the Binary class into it so it would be able to change the bit if a bit was set. Not a good approach. Do you have any ideas?
Thanks for your help,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "
does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
> Well, I've posted it twice and no one is jumpting to optimize it...

perhaps
> it's just so well working that there's no need? he he
>
>
>
>
> Thanks,
> Shawn
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:Oi**************@TK2MSFTNGP09.phx.gbl...
> > Do post it here!
> > I'd be interested to see, as I'm sure many others would.
> > You'll probably get a ton of optimization hints from it. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> news:OW**************@tk2msftngp13.phx.gbl...
> > > Don't miss a thing, d'ya? =))
> > >
> > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit
65c02, and
> the
> > > 16-bit 65816. In particular, I'm doing this so I can debug (I
have the
> > > ability to modify the assembly language while the debugger is

running
> > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > instructions. I'm not designing it to be a particular piece of

hardware
> > > (such as the Apple, Atari, Commadore, etc.). I can supply it

plugins so
> my
> > > plugins can treat an area of memory as a dedicated "something"
such as
> text
> > > display or graphics display, etc. For now, I'm concerned not
even with
> the
> > > text display. My end result, is actually more akin to being
able to > debug
> > > my NES games I've been into writing lately. Also I want to be ready > with my
> > > own debugger for Andre LaMothe's upcoming XGameStation console

(don't
> know
> > > whether he'll have a debugger or not) but I want a debugger that

allows
> me
> > > to change the code while it's being debugged and my simulator allows for
> it
> > > (as long as the source code file is available -- else if it was

loading
> as a
> > > .bin file then it won't be editable during a debug session).
> > >
> > > So it's really more than a simulator. It's an integrated
> (non-seperable)
> > > assembly, linker, dissassembler, debugger, simulator, etc. kind
of a > beast.
> > > I chose C# because it's not the kind of thing I'd want to do in

VB.NET
> (I'm
> > > primarly a VB.NET programmer) but didn't feel like doing it in

Managed
> C++
> > > but also wanted it to target the .NET framework (as much as is
> reasonable
> > > and I'll find out what that really means when its mature enough to start
> > > using reliably).
> > >
> > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz
CPU with
> > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do

emulating
> the
> > > 16-bit processor as I haven't deciphered the specs for the CPU
yet (the
> > > 8-bit CPU is very easy compared to the 16-bit version of the

processor).
> I
> > > ultimately will make this Interfacable so I can write a Z80
core, as > well.
> > >
> > > These bit manipulations are the utmost inner-loop so I have to make them
> > > tight. Looking at the JIT'd code for these methods seems to be

decent
> but I
> > > have to make too many function calls (read, jumps) for my tastes
but > doesn't
> > > seem to be hurting me at the moment). Since I can't inline in ..NET, > I'll
> > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't > > > generate the best code for the rotates and shifts because I have to > emulate
> > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a > better
> > > way for simulating memory manipulation. Now I'm creating a string of
> (64kb
> > > in length) but manipulating each byte by substrings has got to
be > expensive.
> > > I'm looking into whether a byte array would be better.
> > >
> > >
> > > Thanks for the help,
> > > if interested, I'll post my binary class here for all to see.
> > >
> > > I've completed the Rotates, Shift, converting numerics into binary > (string
> > > of 1's and 0's). I know now have to read the binary back to a

numeric,
> and
> > > allow a way to get/set/toggle individual bits.
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > > news:%2****************@tk2msftngp13.phx.gbl...
> > > > So, you're writing an emulator for which processor??
> > > >
> > > > --
> > > > Bob Powell [MVP]
> > > > C#, System.Drawing
> > > >
> > > > September's edition of Well Formed is now available.
> > > > http://www.bobpowell.net/currentissue.htm
> > > >
> > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > >
> > > >
> > > >
> > > > "Shawn B." <le****@html.com> wrote in message
> > > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > > Greetings,
> > > > >
> > > > > I am simulating an assembly language bit rotation in C# and it works
> > > > > wonderfully
> > > > >
> > > > > ---------
> > > > > ...
> > > > > public uint Value;
> > > > > ...
> > > > > public uint RotateRight(byte count) {
> > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > return Value;
> > > > > }
> > > > > ---------
> > > > >
> > > > > as long is the value is 32-bit or 64-bit (because of a

limitation of
> the
> > > > > shift operator). I need it to work on an 8-bit or 16-bit
value, as
> > > well.
> > > > I
> > > > > presume I'd have to have knowledge of the intended size of
the value
> and
> > > > do
> > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit

position
> and
> > > > clear
> > > > > the extra bits if there are any. That seems like a lot of
work. > > > > >
> > > > > Is there a way to simulate an 8 or 16-bit rotate with a

32-bit or
> 64-bit
> > > > > value?
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #28
I was thinking about that last night but forgot all about it. Now, I have
once again optimized that method and made it exactly what I always wanted it
to be. It will not check to see if the length of the string exceeds the
number of bits it can contain. Instead, it checks that all characters are
1's and 0' and if a 1 bit exceeds SIZE then it returns false. You can pass
a 100 "0"'s but if bit 9 is a 1, then it will return false because it can't
be an 8-bit value. This is in line with the rest of the object that will
allow me to assign a Binary64 to a Binary8 as long as the number is at most
an 8-bit value.

The execption handler is gone. Thanks for the suggestion.

Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:uU****************@TK2MSFTNGP11.phx.gbl...
Another suggestion..
You have a method called IsBinary to determine whether a string is valid and can be parsed. In the framework, methods called IsXXX and return a bool usually won't throw an exception (which you method does). The purpose of these methods (as far as I know) is to avoid catching errors by Exception handling, cause it incurrs such a performance penalty. Remove the exception from this method and return false if the string is too long to be compliant.
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
Robert,

You're first suggestion crossed my mind, I have already started eliminating the temp variables thus far (thus far only in the 8-bit operator overloads region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...
I also plan to move the Shift function logic into the overload to avoid the extra function call.

The GetBit optimization hint you provided is what I've been wanting to do but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like it's necessary. Perhaps I could just make the if based on some calculation and then just throw an exception at the end of the function and avoid that early check, perhaps that would be a better approach. Such as (if (Value == 0 && Value < SIZE-1) {)

There is one thing I need help on.
I don't like the GetBit, SetBit approach. I would rather be able to say x = BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x = x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a reference of the Binary class into it so it would be able to change the bit if a bit was set. Not a good approach. Do you have any ideas?
Thanks for your help,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "
does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
> Well, I've posted it twice and no one is jumpting to optimize it...

perhaps
> it's just so well working that there's no need? he he
>
>
>
>
> Thanks,
> Shawn
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:Oi**************@TK2MSFTNGP09.phx.gbl...
> > Do post it here!
> > I'd be interested to see, as I'm sure many others would.
> > You'll probably get a ton of optimization hints from it. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> news:OW**************@tk2msftngp13.phx.gbl...
> > > Don't miss a thing, d'ya? =))
> > >
> > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit
65c02, and
> the
> > > 16-bit 65816. In particular, I'm doing this so I can debug (I
have the
> > > ability to modify the assembly language while the debugger is

running
> > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > instructions. I'm not designing it to be a particular piece of

hardware
> > > (such as the Apple, Atari, Commadore, etc.). I can supply it

plugins so
> my
> > > plugins can treat an area of memory as a dedicated "something"
such as
> text
> > > display or graphics display, etc. For now, I'm concerned not
even with
> the
> > > text display. My end result, is actually more akin to being
able to > debug
> > > my NES games I've been into writing lately. Also I want to be ready > with my
> > > own debugger for Andre LaMothe's upcoming XGameStation console

(don't
> know
> > > whether he'll have a debugger or not) but I want a debugger that

allows
> me
> > > to change the code while it's being debugged and my simulator allows for
> it
> > > (as long as the source code file is available -- else if it was

loading
> as a
> > > .bin file then it won't be editable during a debug session).
> > >
> > > So it's really more than a simulator. It's an integrated
> (non-seperable)
> > > assembly, linker, dissassembler, debugger, simulator, etc. kind
of a > beast.
> > > I chose C# because it's not the kind of thing I'd want to do in

VB.NET
> (I'm
> > > primarly a VB.NET programmer) but didn't feel like doing it in

Managed
> C++
> > > but also wanted it to target the .NET framework (as much as is
> reasonable
> > > and I'll find out what that really means when its mature enough to start
> > > using reliably).
> > >
> > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz
CPU with
> > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do

emulating
> the
> > > 16-bit processor as I haven't deciphered the specs for the CPU
yet (the
> > > 8-bit CPU is very easy compared to the 16-bit version of the

processor).
> I
> > > ultimately will make this Interfacable so I can write a Z80
core, as > well.
> > >
> > > These bit manipulations are the utmost inner-loop so I have to make them
> > > tight. Looking at the JIT'd code for these methods seems to be

decent
> but I
> > > have to make too many function calls (read, jumps) for my tastes
but > doesn't
> > > seem to be hurting me at the moment). Since I can't inline in ..NET, > I'll
> > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't > > > generate the best code for the rotates and shifts because I have to > emulate
> > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a > better
> > > way for simulating memory manipulation. Now I'm creating a string of
> (64kb
> > > in length) but manipulating each byte by substrings has got to
be > expensive.
> > > I'm looking into whether a byte array would be better.
> > >
> > >
> > > Thanks for the help,
> > > if interested, I'll post my binary class here for all to see.
> > >
> > > I've completed the Rotates, Shift, converting numerics into binary > (string
> > > of 1's and 0's). I know now have to read the binary back to a

numeric,
> and
> > > allow a way to get/set/toggle individual bits.
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > > news:%2****************@tk2msftngp13.phx.gbl...
> > > > So, you're writing an emulator for which processor??
> > > >
> > > > --
> > > > Bob Powell [MVP]
> > > > C#, System.Drawing
> > > >
> > > > September's edition of Well Formed is now available.
> > > > http://www.bobpowell.net/currentissue.htm
> > > >
> > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > >
> > > >
> > > >
> > > > "Shawn B." <le****@html.com> wrote in message
> > > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > > Greetings,
> > > > >
> > > > > I am simulating an assembly language bit rotation in C# and it works
> > > > > wonderfully
> > > > >
> > > > > ---------
> > > > > ...
> > > > > public uint Value;
> > > > > ...
> > > > > public uint RotateRight(byte count) {
> > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > return Value;
> > > > > }
> > > > > ---------
> > > > >
> > > > > as long is the value is 32-bit or 64-bit (because of a

limitation of
> the
> > > > > shift operator). I need it to work on an 8-bit or 16-bit
value, as
> > > well.
> > > > I
> > > > > presume I'd have to have knowledge of the intended size of
the value
> and
> > > > do
> > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit

position
> and
> > > > clear
> > > > > the extra bits if there are any. That seems like a lot of
work. > > > > >
> > > > > Is there a way to simulate an 8 or 16-bit rotate with a

32-bit or
> 64-bit
> > > > > value?
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #29
There is now yet a newer version to check as of the time of this post.
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...
Yes, it's better to just return the value in those cases.
I'm gonna go check the new code now. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:u9**************@TK2MSFTNGP12.phx.gbl...
Robert,

I updated the objects with the opimizations you suggested and a few others I thought were neccessary. I also added an "And" and "Or" method to the
classes. I changed the Rotate and Shift methods to not set Value and then return Value. Now they only return it. The problem was if I said "bin2 = bin.RotateLeft(2);" both bin2 and bin would end up with a new value. I
originally did that because before I was overloading the operators I was
actually just saying "bin.RotateLeft(2);" then later decided to make it
return the new value, as well. Now it is not necessary with all the
overloads. Not very consistant with the rest of the .NET framework so I
corrected it. I put a special thanks in at the end of the ReadMe.

Okay, now, let me know if you have any other ideas or optimization tips =)).

Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "
does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
> Well, I've posted it twice and no one is jumpting to optimize it...

perhaps
> it's just so well working that there's no need? he he
>
>
>
>
> Thanks,
> Shawn
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:Oi**************@TK2MSFTNGP09.phx.gbl...
> > Do post it here!
> > I'd be interested to see, as I'm sure many others would.
> > You'll probably get a ton of optimization hints from it. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> news:OW**************@tk2msftngp13.phx.gbl...
> > > Don't miss a thing, d'ya? =))
> > >
> > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit
65c02, and
> the
> > > 16-bit 65816. In particular, I'm doing this so I can debug (I
have the
> > > ability to modify the assembly language while the debugger is

running
> > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > instructions. I'm not designing it to be a particular piece of

hardware
> > > (such as the Apple, Atari, Commadore, etc.). I can supply it

plugins so
> my
> > > plugins can treat an area of memory as a dedicated "something"
such as
> text
> > > display or graphics display, etc. For now, I'm concerned not
even with
> the
> > > text display. My end result, is actually more akin to being
able to > debug
> > > my NES games I've been into writing lately. Also I want to be ready > with my
> > > own debugger for Andre LaMothe's upcoming XGameStation console

(don't
> know
> > > whether he'll have a debugger or not) but I want a debugger that

allows
> me
> > > to change the code while it's being debugged and my simulator allows for
> it
> > > (as long as the source code file is available -- else if it was

loading
> as a
> > > .bin file then it won't be editable during a debug session).
> > >
> > > So it's really more than a simulator. It's an integrated
> (non-seperable)
> > > assembly, linker, dissassembler, debugger, simulator, etc. kind
of a > beast.
> > > I chose C# because it's not the kind of thing I'd want to do in

VB.NET
> (I'm
> > > primarly a VB.NET programmer) but didn't feel like doing it in

Managed
> C++
> > > but also wanted it to target the .NET framework (as much as is
> reasonable
> > > and I'll find out what that really means when its mature enough to start
> > > using reliably).
> > >
> > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz
CPU with
> > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do

emulating
> the
> > > 16-bit processor as I haven't deciphered the specs for the CPU
yet (the
> > > 8-bit CPU is very easy compared to the 16-bit version of the

processor).
> I
> > > ultimately will make this Interfacable so I can write a Z80
core, as > well.
> > >
> > > These bit manipulations are the utmost inner-loop so I have to make them
> > > tight. Looking at the JIT'd code for these methods seems to be

decent
> but I
> > > have to make too many function calls (read, jumps) for my tastes
but > doesn't
> > > seem to be hurting me at the moment). Since I can't inline in ..NET, > I'll
> > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't > > > generate the best code for the rotates and shifts because I have to > emulate
> > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a > better
> > > way for simulating memory manipulation. Now I'm creating a string of
> (64kb
> > > in length) but manipulating each byte by substrings has got to
be > expensive.
> > > I'm looking into whether a byte array would be better.
> > >
> > >
> > > Thanks for the help,
> > > if interested, I'll post my binary class here for all to see.
> > >
> > > I've completed the Rotates, Shift, converting numerics into binary > (string
> > > of 1's and 0's). I know now have to read the binary back to a

numeric,
> and
> > > allow a way to get/set/toggle individual bits.
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > > news:%2****************@tk2msftngp13.phx.gbl...
> > > > So, you're writing an emulator for which processor??
> > > >
> > > > --
> > > > Bob Powell [MVP]
> > > > C#, System.Drawing
> > > >
> > > > September's edition of Well Formed is now available.
> > > > http://www.bobpowell.net/currentissue.htm
> > > >
> > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > >
> > > >
> > > >
> > > > "Shawn B." <le****@html.com> wrote in message
> > > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > > Greetings,
> > > > >
> > > > > I am simulating an assembly language bit rotation in C# and it works
> > > > > wonderfully
> > > > >
> > > > > ---------
> > > > > ...
> > > > > public uint Value;
> > > > > ...
> > > > > public uint RotateRight(byte count) {
> > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > return Value;
> > > > > }
> > > > > ---------
> > > > >
> > > > > as long is the value is 32-bit or 64-bit (because of a

limitation of
> the
> > > > > shift operator). I need it to work on an 8-bit or 16-bit
value, as
> > > well.
> > > > I
> > > > > presume I'd have to have knowledge of the intended size of
the value
> and
> > > > do
> > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit

position
> and
> > > > clear
> > > > > the extra bits if there are any. That seems like a lot of
work. > > > > >
> > > > > Is there a way to simulate an 8 or 16-bit rotate with a

32-bit or
> 64-bit
> > > > > value?
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #30
I this has been completed. It works great. I would have never thought of
it in this manner and yet it is so simple, I was hoping more for a Bit type
of syntax but this works great. I didn't get rid of GetBit/SetBit (just
made them private) because the code that they perform doesn't "feel" right
in the indexer code, just looks weird. I'll make it look "right" soon but
for now...
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:us**************@TK2MSFTNGP11.phx.gbl...
Hi Shawn,

In GetBit, SetBit or in shift operators, you don't need to check of are shifting 0 steps, just do it anyway. It will return the correct value.
Couldn't you have an indexer on the BinaryXX class itself?
Then you'd say,
BinaryX[0] = 1;
BinaryX[2] = BinaryX[0];

For me, that would make more sense than a separate Bit class.

public int this [int index] // Indexer declaration
{
get
{
//GetBit here
}
set
{
//SetBit here
}
}
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
Robert,

You're first suggestion crossed my mind, I have already started eliminating the temp variables thus far (thus far only in the 8-bit operator overloads region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...
I also plan to move the Shift function logic into the overload to avoid the extra function call.

The GetBit optimization hint you provided is what I've been wanting to do but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like it's necessary. Perhaps I could just make the if based on some calculation and then just throw an exception at the end of the function and avoid that early check, perhaps that would be a better approach. Such as (if (Value == 0 && Value < SIZE-1) {)

There is one thing I need help on.
I don't like the GetBit, SetBit approach. I would rather be able to say x = BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x = x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a reference of the Binary class into it so it would be able to change the bit if a bit was set. Not a good approach. Do you have any ideas?
Thanks for your help,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
Hi Shawn,

I've looked at it, and no optimization suggestions other than
some obvious simple ones:

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() + "
does not exist.");
}
uint val = (uint)Value;
byte shift = (byte)(index - 1);
if (index == 0)
return (int)(val & 0x01);
else
return (int)(val >> shift) & 0x01;
}

This is very verbose, and you're creating lots of temp variables
I'd rather write something like (untested):

public int GetBit(byte index) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
return (int)((Value >> index-1) & 0x01;
}

The check to avoid a shift probably just hurts performance anyway.
Also for SetBit, the two if else must result in unwanted branches:

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
byte shift = index;
if (index == 0) {
if (value == 0)
Value &= unchecked((ushort)~0x01);
else
Value |= 0x01;
}
else {
if (value == 0)
Value &= ((ushort)~(0x01 << shift));
else
Value |= (ushort)(0x01 << shift);
}
}

How about this instead (also untested):

public void SetBit(byte index, int value) {
if (index > (byte)SIZE-1) {
throw new IndexOutOfRangeException("Bit " + index.ToString() +
" does not exist.");
}
if (value == 0)
Value &= ((ushort)~(0x01 << index));
else
Value |= (ushort)(0x01 << index);
}

This is the only type of optimizations I came up with, so I thought it

wasn't worth posting. :)


--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2***************@tk2msftngp13.phx.gbl...
> Well, I've posted it twice and no one is jumpting to optimize it...

perhaps
> it's just so well working that there's no need? he he
>
>
>
>
> Thanks,
> Shawn
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:Oi**************@TK2MSFTNGP09.phx.gbl...
> > Do post it here!
> > I'd be interested to see, as I'm sure many others would.
> > You'll probably get a ton of optimization hints from it. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> news:OW**************@tk2msftngp13.phx.gbl...
> > > Don't miss a thing, d'ya? =))
> > >
> > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit
65c02, and
> the
> > > 16-bit 65816. In particular, I'm doing this so I can debug (I
have the
> > > ability to modify the assembly language while the debugger is

running
> > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > instructions. I'm not designing it to be a particular piece of

hardware
> > > (such as the Apple, Atari, Commadore, etc.). I can supply it

plugins so
> my
> > > plugins can treat an area of memory as a dedicated "something"
such as
> text
> > > display or graphics display, etc. For now, I'm concerned not
even with
> the
> > > text display. My end result, is actually more akin to being
able to > debug
> > > my NES games I've been into writing lately. Also I want to be ready > with my
> > > own debugger for Andre LaMothe's upcoming XGameStation console

(don't
> know
> > > whether he'll have a debugger or not) but I want a debugger that

allows
> me
> > > to change the code while it's being debugged and my simulator allows for
> it
> > > (as long as the source code file is available -- else if it was

loading
> as a
> > > .bin file then it won't be editable during a debug session).
> > >
> > > So it's really more than a simulator. It's an integrated
> (non-seperable)
> > > assembly, linker, dissassembler, debugger, simulator, etc. kind
of a > beast.
> > > I chose C# because it's not the kind of thing I'd want to do in

VB.NET
> (I'm
> > > primarly a VB.NET programmer) but didn't feel like doing it in

Managed
> C++
> > > but also wanted it to target the .NET framework (as much as is
> reasonable
> > > and I'll find out what that really means when its mature enough to start
> > > using reliably).
> > >
> > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz
CPU with
> > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do

emulating
> the
> > > 16-bit processor as I haven't deciphered the specs for the CPU
yet (the
> > > 8-bit CPU is very easy compared to the 16-bit version of the

processor).
> I
> > > ultimately will make this Interfacable so I can write a Z80
core, as > well.
> > >
> > > These bit manipulations are the utmost inner-loop so I have to make them
> > > tight. Looking at the JIT'd code for these methods seems to be

decent
> but I
> > > have to make too many function calls (read, jumps) for my tastes
but > doesn't
> > > seem to be hurting me at the moment). Since I can't inline in ..NET, > I'll
> > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't > > > generate the best code for the rotates and shifts because I have to > emulate
> > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a > better
> > > way for simulating memory manipulation. Now I'm creating a string of
> (64kb
> > > in length) but manipulating each byte by substrings has got to
be > expensive.
> > > I'm looking into whether a byte array would be better.
> > >
> > >
> > > Thanks for the help,
> > > if interested, I'll post my binary class here for all to see.
> > >
> > > I've completed the Rotates, Shift, converting numerics into binary > (string
> > > of 1's and 0's). I know now have to read the binary back to a

numeric,
> and
> > > allow a way to get/set/toggle individual bits.
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > > news:%2****************@tk2msftngp13.phx.gbl...
> > > > So, you're writing an emulator for which processor??
> > > >
> > > > --
> > > > Bob Powell [MVP]
> > > > C#, System.Drawing
> > > >
> > > > September's edition of Well Formed is now available.
> > > > http://www.bobpowell.net/currentissue.htm
> > > >
> > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > >
> > > >
> > > >
> > > > "Shawn B." <le****@html.com> wrote in message
> > > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > > Greetings,
> > > > >
> > > > > I am simulating an assembly language bit rotation in C# and it works
> > > > > wonderfully
> > > > >
> > > > > ---------
> > > > > ...
> > > > > public uint Value;
> > > > > ...
> > > > > public uint RotateRight(byte count) {
> > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > return Value;
> > > > > }
> > > > > ---------
> > > > >
> > > > > as long is the value is 32-bit or 64-bit (because of a

limitation of
> the
> > > > > shift operator). I need it to work on an 8-bit or 16-bit
value, as
> > > well.
> > > > I
> > > > > presume I'd have to have knowledge of the intended size of
the value
> and
> > > > do
> > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit

position
> and
> > > > clear
> > > > > the extra bits if there are any. That seems like a lot of
work. > > > > >
> > > > > Is there a way to simulate an 8 or 16-bit rotate with a

32-bit or
> 64-bit
> > > > > value?
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #31
Hey Shawn,

So now getting or setting bits costs an extra method call? Get used to it and remove the extra method call. :)
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message news:e2*************@TK2MSFTNGP12.phx.gbl...
I this has been completed. It works great. I would have never thought of
it in this manner and yet it is so simple, I was hoping more for a Bit type
of syntax but this works great. I didn't get rid of GetBit/SetBit (just
made them private) because the code that they perform doesn't "feel" right
in the indexer code, just looks weird. I'll make it look "right" soon but
for now...
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:us**************@TK2MSFTNGP11.phx.gbl...
Hi Shawn,

In GetBit, SetBit or in shift operators, you don't need to check of are

shifting 0 steps, just do it anyway. It will return the
correct value.
Couldn't you have an indexer on the BinaryXX class itself?
Then you'd say,
BinaryX[0] = 1;
BinaryX[2] = BinaryX[0];

For me, that would make more sense than a separate Bit class.

public int this [int index] // Indexer declaration
{
get
{
//GetBit here
}
set
{
//SetBit here
}
}
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
Robert,

You're first suggestion crossed my mind, I have already started eliminating the temp variables thus far (thus far only in the 8-bit operator overloads region area). Just be happy you didn't see the earlier version I first
posted, it didn't cast because I kept getting compile errors, it ran
everything through a Convert.ToUIntX() call but I've since removed those...
I also plan to move the Shift function logic into the overload to avoid the extra function call.

The GetBit optimization hint you provided is what I've been wanting to do but didn't quite put it they way you did... I'll try that and see how it
works. I don't know what to do about that check in it, though. I feel like it's necessary. Perhaps I could just make the if based on some calculation and then just throw an exception at the end of the function and avoid that early check, perhaps that would be a better approach. Such as (if (Value == 0 && Value < SIZE-1) {)

There is one thing I need help on.
I don't like the GetBit, SetBit approach. I would rather be able to say x = BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had an
indexer but it had problems. Not the least of which was a circular
reference that ended up being a memory leak, not what I wanted in the
innermost loop of an emulator. The second being that I could easily say x = x.Bit[0] but not easily say x.Bit[0] = 1.

Do you have any ideas how I might approach this? I was using a property
get/set at first but that didn't work, instead I just created a public
module variable "public Bit Bit;" and then it had a constructor to pass a reference of the Binary class into it so it would be able to change the bit if a bit was set. Not a good approach. Do you have any ideas?
Thanks for your help,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:O9*************@TK2MSFTNGP10.phx.gbl...
> Hi Shawn,
>
> I've looked at it, and no optimization suggestions other than
> some obvious simple ones:
>
> public int GetBit(byte index) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
> }
> uint val = (uint)Value;
> byte shift = (byte)(index - 1);
> if (index == 0)
> return (int)(val & 0x01);
> else
> return (int)(val >> shift) & 0x01;
> }
>
> This is very verbose, and you're creating lots of temp variables
> I'd rather write something like (untested):
>
> public int GetBit(byte index) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
> }
> return (int)((Value >> index-1) & 0x01;
> }
>
> The check to avoid a shift probably just hurts performance anyway.
> Also for SetBit, the two if else must result in unwanted branches:
>
> public void SetBit(byte index, int value) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
> }
> byte shift = index;
> if (index == 0) {
> if (value == 0)
> Value &= unchecked((ushort)~0x01);
> else
> Value |= 0x01;
> }
> else {
> if (value == 0)
> Value &= ((ushort)~(0x01 << shift));
> else
> Value |= (ushort)(0x01 << shift);
> }
> }
>
> How about this instead (also untested):
>
> public void SetBit(byte index, int value) {
> if (index > (byte)SIZE-1) {
> throw new IndexOutOfRangeException("Bit " + index.ToString() + " does not exist.");
> }
> if (value == 0)
> Value &= ((ushort)~(0x01 << index));
> else
> Value |= (ushort)(0x01 << index);
> }
>
> This is the only type of optimizations I came up with, so I thought it
wasn't worth posting. :)
>
>
> --
> Robert Jeppesen
> robert.jeppesen%at%durius-dot-se
>
>
> "Shawn B." <le****@html.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
> > Well, I've posted it twice and no one is jumpting to optimize it...
perhaps
> > it's just so well working that there's no need? he he
> >
> >
> >
> >
> > Thanks,
> > Shawn
> >
> > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > news:Oi**************@TK2MSFTNGP09.phx.gbl...
> > > Do post it here!
> > > I'd be interested to see, as I'm sure many others would.
> > > You'll probably get a ton of optimization hints from it. :)
> > >
> > >
> > > --
> > > Robert Jeppesen
> > > robert.jeppesen%at%durius-dot-se
> > >
> > >
> > > "Shawn B." <le****@html.com> wrote in message
> > news:OW**************@tk2msftngp13.phx.gbl...
> > > > Don't miss a thing, d'ya? =))
> > > >
> > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit 65c02, and
> > the
> > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have the
> > > > ability to modify the assembly language while the debugger is
running
> > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw
> > > > instructions. I'm not designing it to be a particular piece of
hardware
> > > > (such as the Apple, Atari, Commadore, etc.). I can supply it
plugins so
> > my
> > > > plugins can treat an area of memory as a dedicated "something" such as
> > text
> > > > display or graphics display, etc. For now, I'm concerned not even with
> > the
> > > > text display. My end result, is actually more akin to being able to > > debug
> > > > my NES games I've been into writing lately. Also I want to be ready > > with my
> > > > own debugger for Andre LaMothe's upcoming XGameStation console
(don't
> > know
> > > > whether he'll have a debugger or not) but I want a debugger that
allows
> > me
> > > > to change the code while it's being debugged and my simulator allows for
> > it
> > > > (as long as the source code file is available -- else if it was
loading
> > as a
> > > > .bin file then it won't be editable during a debug session).
> > > >
> > > > So it's really more than a simulator. It's an integrated
> > (non-seperable)
> > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a > > beast.
> > > > I chose C# because it's not the kind of thing I'd want to do in
VB.NET
> > (I'm
> > > > primarly a VB.NET programmer) but didn't feel like doing it in
Managed
> > C++
> > > > but also wanted it to target the .NET framework (as much as is
> > reasonable
> > > > and I'll find out what that really means when its mature enough to start
> > > > using reliably).
> > > >
> > > > I can do a decent job emulating 1-5MHz in C# on my PIII 1000MHz CPU with
> > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll do
emulating
> > the
> > > > 16-bit processor as I haven't deciphered the specs for the CPU yet (the
> > > > 8-bit CPU is very easy compared to the 16-bit version of the
processor).
> > I
> > > > ultimately will make this Interfacable so I can write a Z80 core, as > > well.
> > > >
> > > > These bit manipulations are the utmost inner-loop so I have to make them
> > > > tight. Looking at the JIT'd code for these methods seems to be
decent
> > but I
> > > > have to make too many function calls (read, jumps) for my tastes but > > doesn't
> > > > seem to be hurting me at the moment). Since I can't inline in .NET, > > I'll
> > > > have to live with it. Also, being an 8-bit and 16-bit CPU, it can't > > > > generate the best code for the rotates and shifts because I have to > > emulate
> > > > 8-bit and 16-bit shifts and rotates. I'm also trying to work out a > > better
> > > > way for simulating memory manipulation. Now I'm creating a string of
> > (64kb
> > > > in length) but manipulating each byte by substrings has got to be > > expensive.
> > > > I'm looking into whether a byte array would be better.
> > > >
> > > >
> > > > Thanks for the help,
> > > > if interested, I'll post my binary class here for all to see.
> > > >
> > > > I've completed the Rotates, Shift, converting numerics into binary > > (string
> > > > of 1's and 0's). I know now have to read the binary back to a
numeric,
> > and
> > > > allow a way to get/set/toggle individual bits.
> > > >
> > > >
> > > > Thanks,
> > > > Shawn
> > > >
> > > >
> > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > > > news:%2****************@tk2msftngp13.phx.gbl...
> > > > > So, you're writing an emulator for which processor??
> > > > >
> > > > > --
> > > > > Bob Powell [MVP]
> > > > > C#, System.Drawing
> > > > >
> > > > > September's edition of Well Formed is now available.
> > > > > http://www.bobpowell.net/currentissue.htm
> > > > >
> > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > >
> > > > >
> > > > >
> > > > > "Shawn B." <le****@html.com> wrote in message
> > > > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > > > Greetings,
> > > > > >
> > > > > > I am simulating an assembly language bit rotation in C# and it works
> > > > > > wonderfully
> > > > > >
> > > > > > ---------
> > > > > > ...
> > > > > > public uint Value;
> > > > > > ...
> > > > > > public uint RotateRight(byte count) {
> > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > return Value;
> > > > > > }
> > > > > > ---------
> > > > > >
> > > > > > as long is the value is 32-bit or 64-bit (because of a
limitation of
> > the
> > > > > > shift operator). I need it to work on an 8-bit or 16-bit value, as
> > > > well.
> > > > > I
> > > > > > presume I'd have to have knowledge of the intended size of the value
> > and
> > > > > do
> > > > > > the shift and move the bit and the 0 or 7, or 0 or 15 bit
position
> > and
> > > > > clear
> > > > > > the extra bits if there are any. That seems like a lot of work. > > > > > >
> > > > > > Is there a way to simulate an 8 or 16-bit rotate with a 32-bit or
> > 64-bit
> > > > > > value?
> > > > > >
> > > > > >
> > > > > > Thanks,
> > > > > > Shawn
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #32
This extra method call isn't the performance bottleneck, the real kicker
that I'm trying to solve is the fact that this value is immutable. It has
to return a new instance of the class everytime you do something with it.
MS solved that with the string object by providing us a stringbuilder class.
I'd like to be able to make this class mutable, as well.

And no, my intentions are not to keep that extra method call for the set/get
bit, and for the shifts either.
Thanks,
Shawn

"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:eb**************@TK2MSFTNGP11.phx.gbl...
Hey Shawn,

So now getting or setting bits costs an extra method call? Get used to it and remove the extra method call. :)

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:e2*************@TK2MSFTNGP12.phx.gbl...
I this has been completed. It works great. I would have never thought of it in this manner and yet it is so simple, I was hoping more for a Bit type of syntax but this works great. I didn't get rid of GetBit/SetBit (just
made them private) because the code that they perform doesn't "feel" right in the indexer code, just looks weird. I'll make it look "right" soon but for now...
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:us**************@TK2MSFTNGP11.phx.gbl...
Hi Shawn,

In GetBit, SetBit or in shift operators, you don't need to check of are
shifting 0 steps, just do it anyway. It will return the
correct value.
Couldn't you have an indexer on the BinaryXX class itself?
Then you'd say,
BinaryX[0] = 1;
BinaryX[2] = BinaryX[0];

For me, that would make more sense than a separate Bit class.

public int this [int index] // Indexer declaration
{
get
{
//GetBit here
}
set
{
//SetBit here
}
}
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
> Robert,
>
> You're first suggestion crossed my mind, I have already started

eliminating
> the temp variables thus far (thus far only in the 8-bit operator

overloads
> region area). Just be happy you didn't see the earlier version I
first > posted, it didn't cast because I kept getting compile errors, it ran
> everything through a Convert.ToUIntX() call but I've since removed

those...
>
> I also plan to move the Shift function logic into the overload to avoid the
> extra function call.
>
> The GetBit optimization hint you provided is what I've been wanting
to do
> but didn't quite put it they way you did... I'll try that and see
how it > works. I don't know what to do about that check in it, though. I feel like
> it's necessary. Perhaps I could just make the if based on some

calculation
> and then just throw an exception at the end of the function and
avoid that
> early check, perhaps that would be a better approach. Such as (if

(Value ==
> 0 && Value < SIZE-1) {)
>
> There is one thing I need help on.
>
>
> I don't like the GetBit, SetBit approach. I would rather be able to
say x =
> BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had
an > indexer but it had problems. Not the least of which was a circular
> reference that ended up being a memory leak, not what I wanted in the > innermost loop of an emulator. The second being that I could easily say x =
> x.Bit[0] but not easily say x.Bit[0] = 1.
>
> Do you have any ideas how I might approach this? I was using a
property > get/set at first but that didn't work, instead I just created a public > module variable "public Bit Bit;" and then it had a constructor to pass a
> reference of the Binary class into it so it would be able to change
the bit
> if a bit was set. Not a good approach. Do you have any ideas?
>
>
> Thanks for your help,
> Shawn
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:O9*************@TK2MSFTNGP10.phx.gbl...
> > Hi Shawn,
> >
> > I've looked at it, and no optimization suggestions other than
> > some obvious simple ones:
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " +
index.ToString() + "
> does not exist.");
> > }
> > uint val = (uint)Value;
> > byte shift = (byte)(index - 1);
> > if (index == 0)
> > return (int)(val & 0x01);
> > else
> > return (int)(val >> shift) & 0x01;
> > }
> >
> > This is very verbose, and you're creating lots of temp variables
> > I'd rather write something like (untested):
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " +
index.ToString() + "
> does not exist.");
> > }
> > return (int)((Value >> index-1) & 0x01;
> > }
> >
> > The check to avoid a shift probably just hurts performance anyway.
> > Also for SetBit, the two if else must result in unwanted branches:
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " +
index.ToString() + "
> does not exist.");
> > }
> > byte shift = index;
> > if (index == 0) {
> > if (value == 0)
> > Value &= unchecked((ushort)~0x01);
> > else
> > Value |= 0x01;
> > }
> > else {
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << shift));
> > else
> > Value |= (ushort)(0x01 << shift);
> > }
> > }
> >
> > How about this instead (also untested):
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " +
index.ToString() + "
> does not exist.");
> > }
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << index));
> > else
> > Value |= (ushort)(0x01 << index);
> > }
> >
> > This is the only type of optimizations I came up with, so I
thought it > wasn't worth posting. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> news:%2***************@tk2msftngp13.phx.gbl...
> > > Well, I've posted it twice and no one is jumpting to optimize it... > perhaps
> > > it's just so well working that there's no need? he he
> > >
> > >
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > news:Oi**************@TK2MSFTNGP09.phx.gbl...
> > > > Do post it here!
> > > > I'd be interested to see, as I'm sure many others would.
> > > > You'll probably get a ton of optimization hints from it. :)
> > > >
> > > >
> > > > --
> > > > Robert Jeppesen
> > > > robert.jeppesen%at%durius-dot-se
> > > >
> > > >
> > > > "Shawn B." <le****@html.com> wrote in message
> > > news:OW**************@tk2msftngp13.phx.gbl...
> > > > > Don't miss a thing, d'ya? =))
> > > > >
> > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit

65c02,
> and
> > > the
> > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have
> the
> > > > > ability to modify the assembly language while the debugger
is > running
> > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw > > > > > instructions. I'm not designing it to be a particular piece of > hardware
> > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it > plugins so
> > > my
> > > > > plugins can treat an area of memory as a dedicated "something" such
> as
> > > text
> > > > > display or graphics display, etc. For now, I'm concerned
not even
> with
> > > the
> > > > > text display. My end result, is actually more akin to being

able to
> > > debug
> > > > > my NES games I've been into writing lately. Also I want to
be ready
> > > with my
> > > > > own debugger for Andre LaMothe's upcoming XGameStation
console > (don't
> > > know
> > > > > whether he'll have a debugger or not) but I want a debugger that > allows
> > > me
> > > > > to change the code while it's being debugged and my simulator allows
> for
> > > it
> > > > > (as long as the source code file is available -- else if it
was > loading
> > > as a
> > > > > .bin file then it won't be editable during a debug session).
> > > > >
> > > > > So it's really more than a simulator. It's an integrated
> > > (non-seperable)
> > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
> > > beast.
> > > > > I chose C# because it's not the kind of thing I'd want to do
in > VB.NET
> > > (I'm
> > > > > primarly a VB.NET programmer) but didn't feel like doing it in > Managed
> > > C++
> > > > > but also wanted it to target the .NET framework (as much as is > > > reasonable
> > > > > and I'll find out what that really means when its mature enough to
> start
> > > > > using reliably).
> > > > >
> > > > > I can do a decent job emulating 1-5MHz in C# on my PIII
1000MHz CPU
> with
> > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll
do > emulating
> > > the
> > > > > 16-bit processor as I haven't deciphered the specs for the CPU yet
> (the
> > > > > 8-bit CPU is very easy compared to the 16-bit version of the
> processor).
> > > I
> > > > > ultimately will make this Interfacable so I can write a Z80

core, as
> > > well.
> > > > >
> > > > > These bit manipulations are the utmost inner-loop so I have
to make
> them
> > > > > tight. Looking at the JIT'd code for these methods seems to
be > decent
> > > but I
> > > > > have to make too many function calls (read, jumps) for my tastes but
> > > doesn't
> > > > > seem to be hurting me at the moment). Since I can't inline
in .NET,
> > > I'll
> > > > > have to live with it. Also, being an 8-bit and 16-bit CPU,
it can't
> > > > > generate the best code for the rotates and shifts because I
have to
> > > emulate
> > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to
work out a
> > > better
> > > > > way for simulating memory manipulation. Now I'm creating a

string
> of
> > > (64kb
> > > > > in length) but manipulating each byte by substrings has got
to be
> > > expensive.
> > > > > I'm looking into whether a byte array would be better.
> > > > >
> > > > >
> > > > > Thanks for the help,
> > > > > if interested, I'll post my binary class here for all to
see. > > > > >
> > > > > I've completed the Rotates, Shift, converting numerics into

binary
> > > (string
> > > > > of 1's and 0's). I know now have to read the binary back to a > numeric,
> > > and
> > > > > allow a way to get/set/toggle individual bits.
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in

message
> > > > > news:%2****************@tk2msftngp13.phx.gbl...
> > > > > > So, you're writing an emulator for which processor??
> > > > > >
> > > > > > --
> > > > > > Bob Powell [MVP]
> > > > > > C#, System.Drawing
> > > > > >
> > > > > > September's edition of Well Formed is now available.
> > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > >
> > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > >
> > > > > >
> > > > > >
> > > > > > "Shawn B." <le****@html.com> wrote in message
> > > > > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > > > > Greetings,
> > > > > > >
> > > > > > > I am simulating an assembly language bit rotation in C# and it
> works
> > > > > > > wonderfully
> > > > > > >
> > > > > > > ---------
> > > > > > > ...
> > > > > > > public uint Value;
> > > > > > > ...
> > > > > > > public uint RotateRight(byte count) {
> > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > return Value;
> > > > > > > }
> > > > > > > ---------
> > > > > > >
> > > > > > > as long is the value is 32-bit or 64-bit (because of a
> limitation of
> > > the
> > > > > > > shift operator). I need it to work on an 8-bit or
16-bit value,
> as
> > > > > well.
> > > > > > I
> > > > > > > presume I'd have to have knowledge of the intended size
of the
> value
> > > and
> > > > > > do
> > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15
bit > position
> > > and
> > > > > > clear
> > > > > > > the extra bits if there are any. That seems like a lot

of work.
> > > > > > >
> > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a

32-bit
> or
> > > 64-bit
> > > > > > > value?
> > > > > > >
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Shawn
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #33
Okay, check out the new files. I removed the shift and the get/set bit
methods and they now take place without the overhead of a jump.
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:eb**************@TK2MSFTNGP11.phx.gbl...
Hey Shawn,

So now getting or setting bits costs an extra method call? Get used to it and remove the extra method call. :)

--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:e2*************@TK2MSFTNGP12.phx.gbl...
I this has been completed. It works great. I would have never thought of it in this manner and yet it is so simple, I was hoping more for a Bit type of syntax but this works great. I didn't get rid of GetBit/SetBit (just
made them private) because the code that they perform doesn't "feel" right in the indexer code, just looks weird. I'll make it look "right" soon but for now...
Thanks,
Shawn
"Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
news:us**************@TK2MSFTNGP11.phx.gbl...
Hi Shawn,

In GetBit, SetBit or in shift operators, you don't need to check of are
shifting 0 steps, just do it anyway. It will return the
correct value.
Couldn't you have an indexer on the BinaryXX class itself?
Then you'd say,
BinaryX[0] = 1;
BinaryX[2] = BinaryX[0];

For me, that would make more sense than a separate Bit class.

public int this [int index] // Indexer declaration
{
get
{
//GetBit here
}
set
{
//SetBit here
}
}
--
Robert Jeppesen
robert.jeppesen%at%durius-dot-se
"Shawn B." <le****@html.com> wrote in message

news:%2****************@TK2MSFTNGP11.phx.gbl...
> Robert,
>
> You're first suggestion crossed my mind, I have already started

eliminating
> the temp variables thus far (thus far only in the 8-bit operator

overloads
> region area). Just be happy you didn't see the earlier version I
first > posted, it didn't cast because I kept getting compile errors, it ran
> everything through a Convert.ToUIntX() call but I've since removed

those...
>
> I also plan to move the Shift function logic into the overload to avoid the
> extra function call.
>
> The GetBit optimization hint you provided is what I've been wanting
to do
> but didn't quite put it they way you did... I'll try that and see
how it > works. I don't know what to do about that check in it, though. I feel like
> it's necessary. Perhaps I could just make the if based on some

calculation
> and then just throw an exception at the end of the function and
avoid that
> early check, perhaps that would be a better approach. Such as (if

(Value ==
> 0 && Value < SIZE-1) {)
>
> There is one thing I need help on.
>
>
> I don't like the GetBit, SetBit approach. I would rather be able to
say x =
> BinaryX.Bit[0]; BinaryX.Bit[0] = 1; I created a Bit class that had
an > indexer but it had problems. Not the least of which was a circular
> reference that ended up being a memory leak, not what I wanted in the > innermost loop of an emulator. The second being that I could easily say x =
> x.Bit[0] but not easily say x.Bit[0] = 1.
>
> Do you have any ideas how I might approach this? I was using a
property > get/set at first but that didn't work, instead I just created a public > module variable "public Bit Bit;" and then it had a constructor to pass a
> reference of the Binary class into it so it would be able to change
the bit
> if a bit was set. Not a good approach. Do you have any ideas?
>
>
> Thanks for your help,
> Shawn
>
>
> "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> news:O9*************@TK2MSFTNGP10.phx.gbl...
> > Hi Shawn,
> >
> > I've looked at it, and no optimization suggestions other than
> > some obvious simple ones:
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " +
index.ToString() + "
> does not exist.");
> > }
> > uint val = (uint)Value;
> > byte shift = (byte)(index - 1);
> > if (index == 0)
> > return (int)(val & 0x01);
> > else
> > return (int)(val >> shift) & 0x01;
> > }
> >
> > This is very verbose, and you're creating lots of temp variables
> > I'd rather write something like (untested):
> >
> > public int GetBit(byte index) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " +
index.ToString() + "
> does not exist.");
> > }
> > return (int)((Value >> index-1) & 0x01;
> > }
> >
> > The check to avoid a shift probably just hurts performance anyway.
> > Also for SetBit, the two if else must result in unwanted branches:
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " +
index.ToString() + "
> does not exist.");
> > }
> > byte shift = index;
> > if (index == 0) {
> > if (value == 0)
> > Value &= unchecked((ushort)~0x01);
> > else
> > Value |= 0x01;
> > }
> > else {
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << shift));
> > else
> > Value |= (ushort)(0x01 << shift);
> > }
> > }
> >
> > How about this instead (also untested):
> >
> > public void SetBit(byte index, int value) {
> > if (index > (byte)SIZE-1) {
> > throw new IndexOutOfRangeException("Bit " +
index.ToString() + "
> does not exist.");
> > }
> > if (value == 0)
> > Value &= ((ushort)~(0x01 << index));
> > else
> > Value |= (ushort)(0x01 << index);
> > }
> >
> > This is the only type of optimizations I came up with, so I
thought it > wasn't worth posting. :)
> >
> >
> > --
> > Robert Jeppesen
> > robert.jeppesen%at%durius-dot-se
> >
> >
> > "Shawn B." <le****@html.com> wrote in message
> news:%2***************@tk2msftngp13.phx.gbl...
> > > Well, I've posted it twice and no one is jumpting to optimize it... > perhaps
> > > it's just so well working that there's no need? he he
> > >
> > >
> > >
> > >
> > > Thanks,
> > > Shawn
> > >
> > > "Robert Jeppesen" <robert.jeppesen(#)durius.se> wrote in message
> > > news:Oi**************@TK2MSFTNGP09.phx.gbl...
> > > > Do post it here!
> > > > I'd be interested to see, as I'm sure many others would.
> > > > You'll probably get a ton of optimization hints from it. :)
> > > >
> > > >
> > > > --
> > > > Robert Jeppesen
> > > > robert.jeppesen%at%durius-dot-se
> > > >
> > > >
> > > > "Shawn B." <le****@html.com> wrote in message
> > > news:OW**************@tk2msftngp13.phx.gbl...
> > > > > Don't miss a thing, d'ya? =))
> > > > >
> > > > > I'm writing a processor Simulator for the 8-bit 6502, 8-bit

65c02,
> and
> > > the
> > > > > 16-bit 65816. In particular, I'm doing this so I can debug (I have
> the
> > > > > ability to modify the assembly language while the debugger
is > running
> > > > > (similar to VB6 and VC++ Edit-and-Continue). I can only see raw > > > > > instructions. I'm not designing it to be a particular piece of > hardware
> > > > > (such as the Apple, Atari, Commadore, etc.). I can supply it > plugins so
> > > my
> > > > > plugins can treat an area of memory as a dedicated "something" such
> as
> > > text
> > > > > display or graphics display, etc. For now, I'm concerned
not even
> with
> > > the
> > > > > text display. My end result, is actually more akin to being

able to
> > > debug
> > > > > my NES games I've been into writing lately. Also I want to
be ready
> > > with my
> > > > > own debugger for Andre LaMothe's upcoming XGameStation
console > (don't
> > > know
> > > > > whether he'll have a debugger or not) but I want a debugger that > allows
> > > me
> > > > > to change the code while it's being debugged and my simulator allows
> for
> > > it
> > > > > (as long as the source code file is available -- else if it
was > loading
> > > as a
> > > > > .bin file then it won't be editable during a debug session).
> > > > >
> > > > > So it's really more than a simulator. It's an integrated
> > > (non-seperable)
> > > > > assembly, linker, dissassembler, debugger, simulator, etc. kind of a
> > > beast.
> > > > > I chose C# because it's not the kind of thing I'd want to do
in > VB.NET
> > > (I'm
> > > > > primarly a VB.NET programmer) but didn't feel like doing it in > Managed
> > > C++
> > > > > but also wanted it to target the .NET framework (as much as is > > > reasonable
> > > > > and I'll find out what that really means when its mature enough to
> start
> > > > > using reliably).
> > > > >
> > > > > I can do a decent job emulating 1-5MHz in C# on my PIII
1000MHz CPU
> with
> > > > > GeForce II 64MB and 512MB Ram. I don't know how well it'll
do > emulating
> > > the
> > > > > 16-bit processor as I haven't deciphered the specs for the CPU yet
> (the
> > > > > 8-bit CPU is very easy compared to the 16-bit version of the
> processor).
> > > I
> > > > > ultimately will make this Interfacable so I can write a Z80

core, as
> > > well.
> > > > >
> > > > > These bit manipulations are the utmost inner-loop so I have
to make
> them
> > > > > tight. Looking at the JIT'd code for these methods seems to
be > decent
> > > but I
> > > > > have to make too many function calls (read, jumps) for my tastes but
> > > doesn't
> > > > > seem to be hurting me at the moment). Since I can't inline
in .NET,
> > > I'll
> > > > > have to live with it. Also, being an 8-bit and 16-bit CPU,
it can't
> > > > > generate the best code for the rotates and shifts because I
have to
> > > emulate
> > > > > 8-bit and 16-bit shifts and rotates. I'm also trying to
work out a
> > > better
> > > > > way for simulating memory manipulation. Now I'm creating a

string
> of
> > > (64kb
> > > > > in length) but manipulating each byte by substrings has got
to be
> > > expensive.
> > > > > I'm looking into whether a byte array would be better.
> > > > >
> > > > >
> > > > > Thanks for the help,
> > > > > if interested, I'll post my binary class here for all to
see. > > > > >
> > > > > I've completed the Rotates, Shift, converting numerics into

binary
> > > (string
> > > > > of 1's and 0's). I know now have to read the binary back to a > numeric,
> > > and
> > > > > allow a way to get/set/toggle individual bits.
> > > > >
> > > > >
> > > > > Thanks,
> > > > > Shawn
> > > > >
> > > > >
> > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in

message
> > > > > news:%2****************@tk2msftngp13.phx.gbl...
> > > > > > So, you're writing an emulator for which processor??
> > > > > >
> > > > > > --
> > > > > > Bob Powell [MVP]
> > > > > > C#, System.Drawing
> > > > > >
> > > > > > September's edition of Well Formed is now available.
> > > > > > http://www.bobpowell.net/currentissue.htm
> > > > > >
> > > > > > Answer those GDI+ questions with the GDI+ FAQ
> > > > > > http://www.bobpowell.net/gdiplus_faq.htm
> > > > > >
> > > > > >
> > > > > >
> > > > > > "Shawn B." <le****@html.com> wrote in message
> > > > > > news:O9*************@TK2MSFTNGP11.phx.gbl...
> > > > > > > Greetings,
> > > > > > >
> > > > > > > I am simulating an assembly language bit rotation in C# and it
> works
> > > > > > > wonderfully
> > > > > > >
> > > > > > > ---------
> > > > > > > ...
> > > > > > > public uint Value;
> > > > > > > ...
> > > > > > > public uint RotateRight(byte count) {
> > > > > > > Value = ((Value >> count) | (Value << (32-count)));
> > > > > > > return Value;
> > > > > > > }
> > > > > > > ---------
> > > > > > >
> > > > > > > as long is the value is 32-bit or 64-bit (because of a
> limitation of
> > > the
> > > > > > > shift operator). I need it to work on an 8-bit or
16-bit value,
> as
> > > > > well.
> > > > > > I
> > > > > > > presume I'd have to have knowledge of the intended size
of the
> value
> > > and
> > > > > > do
> > > > > > > the shift and move the bit and the 0 or 7, or 0 or 15
bit > position
> > > and
> > > > > > clear
> > > > > > > the extra bits if there are any. That seems like a lot

of work.
> > > > > > >
> > > > > > > Is there a way to simulate an 8 or 16-bit rotate with a

32-bit
> or
> > > 64-bit
> > > > > > > value?
> > > > > > >
> > > > > > >
> > > > > > > Thanks,
> > > > > > > Shawn
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 15 '05 #34

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

Similar topics

6
by: Ramtin Kazemi | last post by:
Hi How can i perform bitwise rotation in C#?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.